Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
hashin' nums
(version: 6)
Comparing performance of:
h1 vs h2 vs h3 vs h4 vs asm.js
Created:
8 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script> 'use strict'; const float64 = (a, b) => { let x = new Uint32Array(2); let y = new Float64Array(x.buffer); x[0] = a|0; x[1] = b|0; return y[0]; }; const float32 = (a) => { let x = new Uint32Array(1); let y = new Float64Array(x.buffer); x[0] = a|0; return y[0]; }; const floatParts = (n) => { let x = new Uint32Array(2); let y = new Float64Array(x.buffer); y[0] = n; return [...x]; }; const randNum = () => float64( (Math.random() * 0xffffffff)|0, (Math.random() * 0xffffffff)|0); const randSpecial = () => float64( (Math.random() * 0xffffffff)|0, (Math.random() * 0xffffffff)|0b01111111111100000000000000000000); const nums = [...Array(5000)] .map(randNum) .concat([NaN, Infinity, -Infinity, -0, +0]); const h1 = function(n) { 'use strict'; const x = new Uint32Array(2); const y = new Float64Array(x.buffer); y[0] = n; return x[0] ^ x[1]; }; const h2x = new Uint32Array(2); const h2y = new Float64Array(h2x.buffer); const h2 = function(n) { 'use strict'; h2y[0] = n; return h2x[0] ^ h2x[1]; }; const h3x = new Uint32Array(2); const h3y = new DataView(h3x.buffer); const h3 = function(n) { 'use strict'; h3y.setFloat64(0, n, true); return h3x[0] ^ h3x[1]; }; const h4 = { f(n) { 'use strict'; this.floatArr[0] = n; return this.intArr[0] ^ this.intArr[1]; } }; h4.intArr = new Uint32Array(2); h4.floatArr = new Float64Array(h4.intArr.buffer); const hashModule = function(stdlib, foreign, buffer) { 'use asm'; const u32s = new stdlib.Uint32Array(buffer); const f64s = new stdlib.Float64Array(buffer); function hash(n) { n = +n; f64s[0] = n; return u32s[0] ^ u32s[1]; }; return {hash : hash}; }; const asmjsHash = hashModule(window, null, new ArrayBuffer(2**16)).hash; </script>
Tests:
h1
let sum = 0; for (let i = 0, len = nums.length; i < len; ++i) { sum += h1(nums[i]); };
h2
let sum = 0; for (let i = 0, len = nums.length; i < len; ++i) { sum += h2(nums[i]); };
h3
let sum = 0; for (let i = 0, len = nums.length; i < len; ++i) { sum += h3(nums[i]); };
h4
let sum = 0; for (let i = 0, len = nums.length; i < len; ++i) { sum += h4.f(nums[i]); };
asm.js
let sum = 0; for (let i = 0, len = nums.length; i < len; ++i) { sum += asmjsHash(nums[i]); };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
h1
h2
h3
h4
asm.js
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the benchmark test. **What is being tested?** The benchmark tests the performance of different hash functions implemented in various ways: 1. `h1` - uses a simple bitwise XOR operation on two 32-bit unsigned integers to produce a 64-bit signed integer hash. 2. `h2` - also uses a bitwise XOR operation, but with a slight modification to produce a 64-bit signed integer hash using two 32-bit unsigned integers stored in separate arrays (`h2x` and `h2y`). 3. `h3` - uses the `DataView` API to convert a 64-bit float into a 32-bit signed integer, which is then XORed with another 32-bit signed integer. 4. `h4` - defines an object with two properties (`intArr` and `floatArr`) that store two 32-bit unsigned integers and one 64-bit float respectively. The hash function is implemented by XORing the first element of these arrays. The benchmark also tests a custom, compiled-in hash function written in asm.js (Assembly JavaScript), which uses a similar approach to `h1` but optimized for performance. **Options compared** The main options being compared are: * Implementation: different ways of implementing hash functions using bitwise XOR operations. * Memory layout: the way data is stored and accessed (e.g., using separate arrays vs. a single array with two elements). * Performance: the speed at which each hash function can produce a result. **Pros and cons** Here's a brief summary: * **h1**: simple, straightforward implementation, but may be slower due to its simplicity. * **h2**: similar to `h1`, but uses separate arrays to store the two 32-bit unsigned integers, potentially leading to more cache misses. * **h3**: uses `DataView` API, which can lead to performance overhead due to additional memory accesses. However, this implementation may be faster in practice due to optimized native code. * **h4**: object-based implementation that stores data in two separate arrays, similar to `h2`. Performance may suffer from cache misses and additional memory access overhead. * **asm.js hash function**: compiled-in, performance-optimized implementation using a different algorithm. This one is likely to be the fastest. **Latest benchmark result** The latest benchmark results show: * `h1` performing slower than expected, with approximately 214 executions per second on Firefox 56. * `h2` outperforming `h1`, with around 275 executions per second on Firefox 56. * `h3` and `asm.js hash function` performing significantly faster than the others. Keep in mind that these results are specific to a particular browser version (Firefox 56) and platform (Windows 7). The actual performance differences may vary depending on other factors, such as hardware and software configurations.
Related benchmarks:
Spread vs Array.prototype.concat
Array.prototype.concat vs Spread speedtest
Large nested Arrays: Immutable.js vs spread operator
getRandomNumberInRange vs getRandomValueInRange
getRandomNumberInRange vs getRandomValueInRange 5000
Comments
Confirm delete:
Do you really want to delete benchmark?