Javascript hash functions to convert string into integer hash
2015-10-12 17:21
561 查看
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
Here is a direct replacement for Java’s String.hashCode() method
implemented in Javascript.
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.
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.
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.
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:
Let me know, if you have found some mistakes. And follow my blog on twitter.
Posted by: Sergiy Dzysyak
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() methodimplemented 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 magicof 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 happensto 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 hashingalgorithm, 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: Sergiy Dzysyak
相关文章推荐
- js数组的操作
- Jsoup解析网页源码时常用的Element(s)类
- Newtonsoft.Json(Json.Net)学习笔记
- Javascript闭包——懂不懂由你,反正我是懂了
- 模块加载器 project.js
- 关于json数据注释的问题
- JSPservlet跳转页面404错误
- javascript知识整理
- jstat
- LimeJS指南7
- LimeJS指南6
- Json格式获取里面某个字段的数据获取天气接口
- Hammer.js
- Hibernate List 转 Json 报错
- JSP动态include与静态include(2015年10月12日)
- 二、JavaScript语言--JS基础--JavaScript进阶篇--函数
- JS自写带描述标签云
- jstat详解
- 利用jstl:functions截取字符串
- Ajax跨域请求解决方案——jsonp