An incremental implementation of MurmurHash3 for JavaScript
This version works significantly faster than the non-incremental version if you need to hash many small strings into a single hash, since string concatenation (to build the single string to pass the non-incremental version) is fairly costly.
In one case tested, using the incremental version was about 50% faster than concatenating 5-10 strings and then hashing.
import Murmurhash3 from "https://deno.land/x/murmurhash/mod.ts";
const hash = MurmurHash3("string");
// Incrementally add text
hash.hash("more strings");
hash.hash("even more strings");
// All calls can be chained if desired
hash
.hash("and")
.hash("some")
.hash("more");
// Get a result
hash.result();
// returns 0xe4ccfe6b
Get a hash state object, optionally initialized with the given key
and seed
.
seed
must be a positive integer if provided.
const hashState = new MurmurHash3();
Incrementally add key
to the hash.
This can be called as many times as you want for the hash state object,
including after a call to result()
.
Get the result of the hash as a 32-bit positive integer. This performs the tail and finalizer portions of the algorithm, but does not store the result in the state object.
This means that it is perfectly safe to get results and then continue adding strings via hash
.
// Do the whole string at once
new MurmurHash3("this is a test string").result();
// 0x70529328
// Do part of the string, get a result, then the other part
const m = MurmurHash3("this is a");
m.result();
// 0xbfc4f834
m.hash(" test string").result();
// 0x70529328 (same as above)
Reset the state object for reuse, optionally using the given seed
(defaultsto 0
like the constructor).
Heavily inspired by garycourt/murmurhash-js, kazuyukitanimura/murmurhash-js, jensyt/imurmurhash-js.