Skip to content

Instantly share code, notes, and snippets.

@blixt
Created August 2, 2014 15:50
Show Gist options
  • Save blixt/6df90b24099f68075a2c to your computer and use it in GitHub Desktop.
Save blixt/6df90b24099f68075a2c to your computer and use it in GitHub Desktop.
Slow asm.js?
function murmurhash3(stdlib, foreign, heap) {
"use asm";
var int32 = new stdlib.Int32Array(heap);
function murmurhash3_32(idx, len, seed) {
idx = idx | 0;
len = len | 0;
seed = seed | 0;
var h = 0, k = 0, i = 0;
h = seed;
for (;
(i | 0) < (len | 0); i = (i + 1) | 0) {
k = int32[(idx + i) << 2 >> 2] | 0;
k = ((k * 0xCC9E) << 16) + (k * 0x2D51 | 0) | 0;
k = (k << 15) | (k >>> 17);
k = ((k * 0x1B87) << 16) + (k * 0x3593 | 0) | 0;
h = h ^ k;
h = (h << 13) | (h >>> 19);
h = ((h << 2) + h + 0xE6546B64) | 0;
}
h = h ^ (len << 2);
h = h ^ (h >>> 16);
h = ((h * 0x85EB) << 16) + (h * 0xCA6B | 0) | 0;
h = h ^ (h >>> 13);
h = ((h * 0xC2B2) << 16) + (h * 0xAE35 | 0) | 0;
h = h ^ (h >>> 16);
return h | 0;
}
return {
hash32: murmurhash3_32,
};
}
var asmView = new Uint8Array(0x1000);
var asmModule = murmurhash3(window, {}, asmView.buffer);
function prepare(key) {
for (var i = 0; i < key.length; i++) {
asmView[i] = key.charCodeAt(i);
}
// Ensure the integer is reset.
var zeroes = 4 - (key.length & 3);
while (zeroes--) {
asmView[i++] = 0;
}
}
function murmur3_asm(key, seed) {
prepare(key);
return asmModule.hash32(0, key.length + 3 >> 2, seed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment