Skip to content

Instantly share code, notes, and snippets.

@OneCent01
Last active October 7, 2017 00:09
Show Gist options
  • Save OneCent01/c848403cd7a25c0bed2d56700b5e9519 to your computer and use it in GitHub Desktop.
Save OneCent01/c848403cd7a25c0bed2d56700b5e9519 to your computer and use it in GitHub Desktop.
String.prototype.hashCode = function(max){
var hash = 0;
if (!this.length) 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 Math.abs(max?hash%max:hash);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment