Skip to content

Instantly share code, notes, and snippets.

@sdesalas
Last active October 17, 2016 05:25
Show Gist options
  • Save sdesalas/546e624ca87265a3f45be52112356554 to your computer and use it in GitHub Desktop.
Save sdesalas/546e624ca87265a3f45be52112356554 to your computer and use it in GitHub Desktop.
Simple high-performance hashing function, based on Java's `int` Object.hashCode()
String.prototype.hashCode = function(){
var hash = 0;
if (this.length == 0) return hash;
for (var i = 0; i < this.length; i++) {
char = this.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment