Skip to content

Instantly share code, notes, and snippets.

@bitboxx
Created March 12, 2015 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitboxx/6d6cd6ab20ce55ac0e01 to your computer and use it in GitHub Desktop.
Save bitboxx/6d6cd6ab20ce55ac0e01 to your computer and use it in GitHub Desktop.
Quick string hashing Javascript function
// This is a modified version of similar functions found here:
// http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery
// http://jsperf.com/hashing-strings
String.prototype.hash = function () {
var res = 0,
len = this.length;
for (i = 0; i < len; i++) {
res = res * 31 + this.charCodeAt(i);
res = res & res;
}
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment