Skip to content

Instantly share code, notes, and snippets.

@iperelivskiy
Created November 19, 2012 14:39
Show Gist options
  • Save iperelivskiy/4110988 to your computer and use it in GitHub Desktop.
Save iperelivskiy/4110988 to your computer and use it in GitHub Desktop.
JS simple hash function
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);
};
@u01jmg3
Copy link

u01jmg3 commented Apr 24, 2021

@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.

@ArashPartow
Copy link

A comprehensive list of general purpose hash functions and their implementations can found here:

https://www.partow.net/programming/hashfunctions/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment