Skip to content

Instantly share code, notes, and snippets.

@xhinking
Created July 20, 2017 02:23
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 xhinking/0f52dd08aaa0575bc9c21f813742ca10 to your computer and use it in GitHub Desktop.
Save xhinking/0f52dd08aaa0575bc9c21f813742ca10 to your computer and use it in GitHub Desktop.
Java’s String.hashCode()
const hashstr = s => {
let hash = 0;
if (s.length == 0) return hash;
for (let i = 0; i < s.length; i++) {
let char = s.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment