Skip to content

Instantly share code, notes, and snippets.

@lwd-temp
Created June 4, 2023 03:23
Show Gist options
  • Save lwd-temp/adafe86ba0c6f8b2e8b1ff38e6571cca to your computer and use it in GitHub Desktop.
Save lwd-temp/adafe86ba0c6f8b2e8b1ff38e6571cca to your computer and use it in GitHub Desktop.
Returns a hash code from a string
/**
* 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;
}
@lwd-temp
Copy link
Author

lwd-temp commented Jun 4, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment