Repository navigation
mzHash32 #25
Replies: 3 comments
|
I'm testing a further version of mzHash32 (6 - the final one) that further improves the collision count, getting very close to the ideal universal hash function. It's only slightly different, but I'll report it in the issues. |
|
That 3byte back and forth shift is a really creative way to handle the sign bit. Yet, assuming the input bytes are randomly distributed you have to perform 2 unnecessary shifts in 50% of the cases. And in the other 50% you could do a single bitwise OR with a bit mask that has all bits set to 1 in the places where the signed right shift would put them. Maybe I'm wrong, but I think you could save 75% time (minus the cost of performing a |
|
Hey, thanks for writing this up! I was looking for a simple hash function like this! My 2 cents as a sign of appreciation: If you know that the input is guaranteed to be in the range 0-255, then If you're not worried about supporting browsers ten years or older, you can also consider using https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/imul Also, the If we inline everything that would result in this: function mzHash32(bytes, seed) {
let hash = 0x514430D1 ^ seed;
for (let i = 0; i < bytes.length; i++) {
hash = Math.imul(0x3C50DB, bytes[i] - 128) ^ (hash << 1) ^ (hash >>> 1));
}
return hash >>> 0;
}(I removed the initial check because in my codebase I type annotated, and my IDE will shout at me if I don't pass a Thanks again for the initial code! |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
My JavaScript port of mzHash32 v0.5 (2026-01-05).
It works even with pre-ES6 engines like JScript.
Original author is Matteo Zapparoli aka @matteo65.
All reactions