Created
June 4, 2023 03:23
-
-
Save lwd-temp/adafe86ba0c6f8b2e8b1ff38e6571cca to your computer and use it in GitHub Desktop.
Returns a hash code from a string
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
/** | |
* Returns a hash code from a string | |
* @param {String} str The string to hash. | |
* @return {Number} A 32bit integer | |
* @see http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/ | |
*/ | |
function hashCode(str) { | |
let hash = 0; | |
for (let i = 0, len = str.length; i < len; i++) { | |
let chr = str.charCodeAt(i); | |
hash = (hash << 5) - hash + chr; | |
hash |= 0; // Convert to 32bit integer | |
} | |
return hash; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/a/8831937