A JavaScript implementation of the 128bit variant of Murmur3. It was written with the following constraints in mind:
- For the same input, it must produce a hash that is equivalent to:
- It must be usable in the browser
Under the covers this implementation uses Closure Library's goog.math.Long. Either the entire Closure library can be included or a dependency free copy can be used.
Performance tests have not yet been run. There are places in which there are a
few different ways to use goog.math.Long
which may affect performance. These
have not yet been investigated (though some of them have been noted).
The equivalent Java (Guava) and JavaScript are provided as an example:
Java (Guava):
final int seed = 0;
final byte[] bytes = { (byte)0xDE, (byte)0xAD, (byte)0xBE, (byte)0xEF,
(byte)0xFE, (byte)0xED, (byte)0xFA, (byte)0xCE };
com.google.common.hash.HashFunction hashFunction = com.google.common.hash.Hashing.murmur3_128(seed);
com.google.common.hash.HashCode hashCode = hashFunction.newHasher()
.putBytes(bytes)
.hash();
System.err.println(hashCode.asLong());
JavaScript:
var seed = 0;
var rawKey = new ArrayBuffer(8);
var byteView = new Int8Array(rawKey);
byteView[0] = 0xDE; byteView[1] = 0xAD; byteView[2] = 0xBE; byteView[3] = 0xEF;
byteView[4] = 0xFE; byteView[5] = 0xED; byteView[6] = 0xFA; byteView[7] = 0xCE;
console.log(murmur3.hash128(rawKey, seed));
Refer to the unit tests for more examples.
- murmurhash-js only contains the 32bit variant.
- node-murmurhash is based off of murmurhash-js and as a result only contains the 32bit variant.
- murmur3 did not seem to match the Java (Guava) output. (The first 8 bytes would match but the remaining would not.)
- murmurhash3-js only contains the 32bit variant.
- node-murmurhash3 provides both the 32bit and 128bit variants (through its binding to the C++ implementation) but cannot be used in the browser.