Created
June 16, 2020 17:53
-
-
Save ccondry/d5e9bbe07e8a816786476a16a3fe5850 to your computer and use it in GitHub Desktop.
Java-compatible string hash
This file contains hidden or 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
function hashString (s) { | |
if (s.length === 0) { | |
return 0 | |
} | |
let hash = 0 | |
for (let i = 0; i < s.length; i++) { | |
const char = s.charCodeAt(i) | |
hash = (( hash << 5 ) - hash) + char | |
// Convert to 32-bit integer | |
hash = hash & hash | |
} | |
return hash | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment