Skip to content

Instantly share code, notes, and snippets.

@larchanka
Last active January 12, 2018 14:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larchanka/9367841 to your computer and use it in GitHub Desktop.
Save larchanka/9367841 to your computer and use it in GitHub Desktop.
JavaScript: Java String.hashCode() implementation. Source: http://goo.gl/Hyt1ku
String.prototype.hashCode = function() {
var hash = 0;
try {
if (this.length == 0) return hash;
for (i = 0; i < this.length; i++) {
char = this.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
} catch (e) {
throw new Error('hashCode: ' + e);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment