Skip to content

Instantly share code, notes, and snippets.

@vbezhenar
Created August 23, 2022 20:58
Show Gist options
  • Save vbezhenar/03ab89a22128e54872677516952ac5a7 to your computer and use it in GitHub Desktop.
Save vbezhenar/03ab89a22128e54872677516952ac5a7 to your computer and use it in GitHub Desktop.
/**
* Performs Murmur3_32 hashing.
* https://en.wikipedia.org/wiki/MurmurHash
* @param key Int32Array input key
* @param seed int32 input seed
* @returns int32 value
*/
export default function murmur332(key, seed) {
let hash = seed;
/* eslint-disable no-bitwise */
for (let i = 0; i < key.length; i += 1) {
let k = key[i];
k = Math.imul(k, -862048943);
k = (k << 15) | (k >>> 17);
k = Math.imul(k, 461845907);
hash ^= k;
hash = (hash << 13) | (hash >>> 19);
hash = (Math.imul(hash, 5) + -430675100) | 0;
}
hash ^= (key.length << 2);
hash ^= (hash >>> 16);
hash = Math.imul(hash, -2048144789);
hash ^= (hash >>> 13);
hash = Math.imul(hash, -1028477387);
hash ^= (hash >>> 16);
/* eslint-enable no-bitwise */
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment