Skip to content

Instantly share code, notes, and snippets.

@larchanka larchanka/hashcode.js
Last active Jan 12, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.