Created
November 19, 2012 14:39
-
-
Save iperelivskiy/4110988 to your computer and use it in GitHub Desktop.
JS simple hash function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var hash = function(s) { | |
/* Simple hash function. */ | |
var a = 1, c = 0, h, o; | |
if (s) { | |
a = 0; | |
/*jshint plusplus:false bitwise:false*/ | |
for (h = s.length - 1; h >= 0; h--) { | |
o = s.charCodeAt(h); | |
a = (a<<6&268435455) + o + (o<<14); | |
c = a & 266338304; | |
a = c!==0?a^c>>21:a; | |
} | |
} | |
return String(a); | |
}; |
A comprehensive list of general purpose hash functions and their implementations can found here:
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@bryc - super explanation, thank you
Going with the 32-bit hash for now but will change to the 53-bit version if we experience frequent collisions.