Javascript hash functions to convert string into integer hash.

In some cases it is not necesary to transfer a string to the server, as you are not going to process its contents in any way, but you just need to identify this string. I this situation hash functions may help. It is just important to remember that hash functions do not guaranty unique hash valuse. Actually each hashing algorithm will have colisions. It is only way to avoid colisions is to define hashing algorithm for a known finit string range. But in real life strings are unknown in most cases. So here are implementations for some hashing algorithms in Javascript. By the way, if you just want to have a good hash function, and cannot wait, djb2 is one of the best string hash functions i know. it has excellent distribution and speed on many different sets of keys and table sizes. you are not likely to do better with one of the "well known" functions such as PJW, K&R[1], etc

Java String.hashCode() implementation

Here is a direct replacement for Java’s String.hashCode() method implemented in Javascript.

hashCode = function(str){
	var hash = 0;
	if (str.length == 0) return hash;
	for (i = 0; i < str.length; i++) {
		char = str.charCodeAt(i);
		hash = ((hash<<5)-hash)+char;
		hash = hash & hash; // Convert to 32bit integer
	}
	return hash;
}

djb2

This algorithm (k=33) was first reported by dan bernstein many years ago in comp.lang.c. another version of this algorithm (now favored by bernstein) uses xor: hash(i) = hash(i - 1) * 33 ^ str[i]; the magic of number 33 (why it works better than many other constants, prime or not) has never been adequately explained.

djb2Code = function(str){
	var hash = 5381;
	for (i = 0; i < str.length; i++) {
		char = str.charCodeAt(i);
		hash = ((hash << 5) + hash) + char; /* hash * 33 + c */
	}
	return hash;
}

sdbm

This algorithm was created for sdbm (a public-domain reimplementation of ndbm) database library. it was found to do well in scrambling bits, causing better distribution of the keys and fewer splits. it also happens to be a good general hashing function with good distribution. the actual function is hash(i) = hash(i - 1) * 65599 + str[i]; what is included below is the faster version used in gawk. [there is even a faster, duff-device version] the magic constant 65599 was picked out of thin air while experimenting with different constants, and turns out to be a prime. this is one of the algorithms used in berkeley db (see sleepycat ) and elsewhere.

sdbmCode = function(str){
	var hash = 0;
	for (i = 0; i < str.length; i++) {
		char = str.charCodeAt(i);
		hash = char + (hash << 6) + (hash << 16) - hash;
	}
	return hash;
}

lose lose

This hash function appeared in K&R (1st ed) but at least the reader was warned: "This is not the best possible algorithm, but it has the merit of extreme simplicity." This is an understatement; It is a terrible hashing algorithm, and it could have been much better without sacrificing its "extreme simplicity." [see the second edition!] Many C programmers use this function without actually testing it, or checking something like Knuth's Sorting and Searching, so it stuck. It is now found mixed with otherwise respectable code, eg. cnews. sigh.

loseCode = function(str){
	var hash = 0;
	for (i = 0; i < str.length; i++) {
		char = str.charCodeAt(i);
		hash += char;
	}
	return hash;
}

In most cases that is not necesary, but if you want to use hash function as a method of any string you can do the following:

String.prototype.hashCode = function(){
  // Your code here
}

Let me know, if you have found some mistakes. And follow my blog on twitter.

Posted by:
Enjoyed this post? Share and Leave a comment below, thanks! :)