Javascript hash functions to convert string into integer hash

http://erlycoder.com/49/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.

  
  
  1. hashCode = function(str){
  2.     var hash = 0;
  3.     if (str.length == 0) return hash;
  4.     for (i = 0; i < str.length; i++) {
  5.         char = str.charCodeAt(i);
  6.         hash = ((hash<<5)-hash)+char;
  7.         hash = hash & hash; // Convert to 32bit integer
  8.     }
  9.     return hash;
  10. }

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.

  
  
  1. djb2Code = function(str){
  2.     var hash = 5381;
  3.     for (i = 0; i < str.length; i++) {
  4.         char = str.charCodeAt(i);
  5.         hash = ((hash << 5) + hash) + char; /* hash * 33 + c */
  6.     }
  7.     return hash;
  8. }

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.

  
  
  1. sdbmCode = function(str){
  2.     var hash = 0;
  3.     for (i = 0; i < str.length; i++) {
  4.         char = str.charCodeAt(i);
  5.         hash = char + (hash << 6) + (hash << 16) - hash;
  6.     }
  7.     return hash;
  8. }

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.

  
  
  1. loseCode = function(str){
  2.     var hash = 0;
  3.     for (i = 0; i < str.length; i++) {
  4.         char = str.charCodeAt(i);
  5.         hash += char;
  6.     }
  7.     return hash;
  8. }

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:

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

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

Posted by:  Sergiy Dzysyak

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值