Skip to content

Instantly share code, notes, and snippets.

@blixt
Created August 2, 2014 21:00
Show Gist options
  • Save blixt/0ba1209182018aeeac99 to your computer and use it in GitHub Desktop.
Save blixt/0ba1209182018aeeac99 to your computer and use it in GitHub Desktop.
Slow asm.js procedure
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>asm.js</title></head>
<body>
<script>
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);
}
function murmur3(key, seed) {
var remainder, bytes, h1, h1b, c1, c1b, c2, c2b, k1, i;
remainder = key.length & 3; // key.length % 4
bytes = key.length - remainder;
h1 = seed;
c1 = 0xcc9e2d51;
c2 = 0x1b873593;
i = 0;
while (i < bytes) {
k1 =
((key.charCodeAt(i) & 0xff)) |
((key.charCodeAt(++i) & 0xff) << 8) |
((key.charCodeAt(++i) & 0xff) << 16) |
((key.charCodeAt(++i) & 0xff) << 24);
++i;
k1 = ((((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16))) & 0xffffffff;
k1 = (k1 << 15) | (k1 >>> 17);
k1 = ((((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16))) & 0xffffffff;
h1 ^= k1;
h1 = (h1 << 13) | (h1 >>> 19);
h1b = ((((h1 & 0xffff) * 5) + ((((h1 >>> 16) * 5) & 0xffff) << 16))) & 0xffffffff;
h1 = (((h1b & 0xffff) + 0x6b64) + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16));
}
k1 = 0;
switch (remainder) {
case 3:
k1 ^= (key.charCodeAt(i + 2) & 0xff) << 16;
case 2:
k1 ^= (key.charCodeAt(i + 1) & 0xff) << 8;
case 1:
k1 ^= (key.charCodeAt(i) & 0xff);
k1 = (((k1 & 0xffff) * c1) + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff;
k1 = (k1 << 15) | (k1 >>> 17);
k1 = (((k1 & 0xffff) * c2) + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff;
h1 ^= k1;
}
h1 ^= key.length;
h1 ^= h1 >>> 16;
h1 = (((h1 & 0xffff) * 0x85ebca6b) + ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) & 0xffffffff;
h1 ^= h1 >>> 13;
h1 = ((((h1 & 0xffff) * 0xc2b2ae35) + ((((h1 >>> 16) * 0xc2b2ae35) & 0xffff) << 16))) & 0xffffffff;
h1 ^= h1 >>> 16;
return h1 >>> 0;
}
var t1 = +new Date;
for (var i = 0; i < 5000000; i++) murmur3('hello, world!', 0);
var t2 = +new Date;
for (var i = 0; i < 5000000; i++) murmur3_asm('hello, world!', 0);
var t3 = +new Date;
console.log('murmur3:', t2 - t1, 'murmur3_asm:', t3 - t2);
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment