Skip to content

Instantly share code, notes, and snippets.

@ccondry
Created June 16, 2020 17:53
Show Gist options
  • Save ccondry/d5e9bbe07e8a816786476a16a3fe5850 to your computer and use it in GitHub Desktop.
Save ccondry/d5e9bbe07e8a816786476a16a3fe5850 to your computer and use it in GitHub Desktop.
Java-compatible string hash
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