Skip to content

Instantly share code, notes, and snippets.

@yhahn
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yhahn/54d12fa2d98dec5ce10c to your computer and use it in GitHub Desktop.
Save yhahn/54d12fa2d98dec5ce10c to your computer and use it in GitHub Desktop.
fnv-plus collision tests
var fnvplus = require('fnv-plus');
function collisionRate(bits) {
var hashes = {};
var texts = 0;
var collisions = [];
var sample = 1e6;
while (texts < sample) {
var text = Math.random().toString(36);
var hash = fnvplus.hash(text,bits).hex();
if (hashes[hash] === text) {
continue;
} else if (hashes[hash]) {
collisions.push([hashes[hash], text]);
} else {
hashes[hash] = text;
}
texts++;
}
console.log('%d bits, collisions: %s% (%s/%s)', bits, (collisions.length/sample * 100).toFixed(3), collisions.length, sample);
}
collisionRate(32);
collisionRate(52);
collisionRate(64);
// 32 bits, collisions: 0.009% (92/1000000)
// 52 bits, collisions: 85.581% (855814/1000000)
// 64 bits, collisions: 0.000% (0/1000000)
{
"name": "fnvplus-collisions",
"dependencies": {
"fnv-plus": "1.2.x"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment