Created
July 20, 2017 02:23
-
-
Save xhinking/0f52dd08aaa0575bc9c21f813742ca10 to your computer and use it in GitHub Desktop.
Java’s String.hashCode()
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
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