MurmurHash3 32-bit
murmur-32 provides a JavaScript implementation of the MurmurHash3 algorithm for x86 32-bit systems. It's a non-cryptographic hash function known for its speed and good distribution properties, often used in hash tables, Bloom filters, and other data structures where collision resistance is less critical than performance. The current stable version is 1.0.0, released in July 2021. The package maintains a relatively slow release cadence, primarily focusing on stability and adherence to modern JavaScript standards. A key differentiator is its straightforward API for hashing both `ArrayBuffer` and string inputs, with strings being UTF-8 encoded by default since version 0.2.0, providing predictable cross-platform behavior.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require()` to import `murmur-32` in a modern Node.js or browser ESM context after upgrading to version 1.0.0 or later.fixChange your import statement to `import murmur32 from 'murmur-32';`. -
Hash mismatch for string inputs after upgrade
cause Upgrading from `murmur-32` v0.1.0 to v0.2.0 or later, where the string encoding for hash inputs changed from UCS-2 to UTF-8, resulting in different hash values for the same string.fixIf you require the previous UCS-2 encoding behavior, pre-process your string inputs using `array-buffer-from-string`: `import arrayBufferFromString from 'array-buffer-from-string'; murmur32(arrayBufferFromString(yourString, 'ucs2'))`. Otherwise, update your application to expect UTF-8 encoded hash outputs.
Warnings
- breaking Version 1.0.0 converted the package to an ECMAScript Module (ESM) and dropped CommonJS support. Direct `require()` statements will no longer work, and older Node.js versions are unsupported.
- breaking Version 0.2.0 changed the default string encoding from UCS-2 (UTF-16) to UTF-8. If your application was implicitly relying on UCS-2 encoding for string inputs prior to v0.2.0, hash outputs for the same string inputs will now differ.
Install
-
npm install murmur-32 -
yarn add murmur-32 -
pnpm add murmur-32
Imports
- murmur32
const murmur32 = require('murmur-32')import murmur32 from 'murmur-32'
Quickstart
import murmur32 from 'murmur-32';
// Example 1: Hashing a string
const stringKey = 'hello world';
const stringHashBuffer = murmur32(stringKey);
const stringHashValue = new DataView(stringHashBuffer).getUint32(0, true); // Get 32-bit integer, little-endian
console.log(`Hash for "${stringKey}": ${stringHashValue.toString(16).padStart(8, '0')}`);
// Example 2: Hashing an ArrayBuffer
const arrayBufferKey = new TextEncoder().encode('another example').buffer;
const arrayBufferHashBuffer = murmur32(arrayBufferKey);
const arrayBufferHashValue = new DataView(arrayBufferHashBuffer).getUint32(0, true);
console.log(`Hash for "another example" (ArrayBuffer): ${arrayBufferHashValue.toString(16).padStart(8, '0')}`);
// The function returns an ArrayBuffer of 4 bytes (32-bit hash).
// You often need to convert it to an integer for practical use.