MurmurHash3 32-bit

1.0.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to compute a 32-bit MurmurHash3 for both string and ArrayBuffer inputs, and how to extract the integer hash value from the returned ArrayBuffer.

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.

view raw JSON →