Skip to content

Instantly share code, notes, and snippets.

@tmlbl
Created May 9, 2014 05:38
Show Gist options
  • Save tmlbl/214def67778db37bb77c to your computer and use it in GitHub Desktop.
Save tmlbl/214def67778db37bb77c to your computer and use it in GitHub Desktop.
Simple hash function in JavaScript
String.prototype.hashCode = function(){
var hash = 0;
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;
}
@ArashPartow
Copy link

A comprehensive list of general purpose hash functions and their implementations can found here:

https://www.partow.net/programming/hashfunctions/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment