Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
hashin' nums & squashin' NaNs
(version: 0)
Comparing performance of:
h1 vs h2
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 u32s = new Uint32Array(2); const f64s = new Float64Array(u32s.buffer); const numberHash1 = function() { 'use strict'; f64s[0] = this; f64s[0] = f64s[0]; return u32s[0] ^ u32s[1]; }; const numberHash2 = function() { 'use strict'; f64s[0] = this; const u32s1 = u32s[1]; const mask = ~((u32s1 === 0b11111111111110000000000000000000) << 31); return u32s[0] ^ (mask & u32s1); }; </script>
Tests:
h1
let sum = 0; for (let i = 0, len = nums.length; i < len; ++i) { sum += numberHash1.call(nums[i]); };
h2
let sum = 0; for (let i = 0, len = nums.length; i < len; ++i) { sum += numberHash2.call(nums[i]); };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
h1
h2
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):
I'll break down the benchmark definition and explain what's being tested, compared, and its implications. **Benchmark Definition** The benchmark consists of two test cases (`h1` and `h2`) that measure the performance of JavaScript hash functions on an array of numbers. The numbers include special values like NaN (Not a Number), Infinity, -Infinity, -0, and +0. **Hash Functions** There are two hash functions implemented: 1. `numberHash1`: This function uses bitwise XOR (`^`) to combine two 32-bit integers from the `float64` array into a single 64-bit integer. 2. `numberHash2`: This function uses a similar approach, but with an additional mask to handle cases where the first 32 bits of the input are all zeros (0b11111111111110000000000000000000). **Options Compared** The benchmark compares two approaches: 1. **Simple XOR**: `numberHash1` uses simple bitwise XOR to combine the numbers. 2. **Masked XOR**: `numberHash2` uses an additional mask to handle cases where the first 32 bits of the input are all zeros. **Pros and Cons** * **Simple XOR (`numberHash1`)**: + Pros: Simple, easy to understand, and fast. + Cons: May produce collisions (different inputs producing the same output) due to the limited range of integers used in JavaScript's `float64`. * **Masked XOR (`numberHash2`)**: + Pros: Reduces collisions by handling special cases explicitly. + Cons: Adds computational overhead with the mask calculation. **Library and Special Features** There is no explicit library mentioned, but it appears that the benchmark uses native JavaScript arrays and data types (e.g., `Uint32Array`, `Float64Array`). No special JavaScript features or syntax are used in this benchmark. The focus is on understanding the simple hash functions and their performance. **Alternatives** Other approaches to hashing numbers could include: * Using a cryptographic hash function like SHA-256 * Implementing a custom, optimized hash function using assembly language (not recommended for production use) * Using a library with built-in hash functions, such as crypto-js Keep in mind that the choice of hashing algorithm depends on the specific requirements and constraints of the application. I hope this explanation helps!
Related benchmarks:
hashin' nums
hashin' nums: .call(n) vs (n)
Large nested Arrays: Immutable.js vs spread operator
Function call vs array access
Comments
Confirm delete:
Do you really want to delete benchmark?