Skip to content

Instantly share code, notes, and snippets.

@komiga
Created August 8, 2013 03:42
Show Gist options
  • Select an option

  • Save komiga/6181269 to your computer and use it in GitHub Desktop.

Select an option

Save komiga/6181269 to your computer and use it in GitHub Desktop.
FNV-1a for JavaScript.
// 32-bit FNV-1a
// Will produce creepy output if input data is not ASCII
function fnv1a(str) {
var OFFSET_BASIS = 0x811c9dc5;
//var PRIME = 0x01000193;
var h = OFFSET_BASIS;
for (var i = 0; i < str.length; ++i) {
h ^= str.charCodeAt(i);
// This should be h *= PRIME;
// but /JavaScript/ so /magic/ instead
h += (h << 1) + (h << 4) + (h << 7) + (h << 8) + (h << 24);
}
return h >>> 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment