admin管理员组文章数量:1410717
I'm doing some caching via javascript. I have a method that takes an array of strings and returns a processed result. I want to create a unique ID from these strings and then use that as the key in an object to store the result. This way the keys in the cache take up as little memory as possible.
In essence I want something like SHA1, but for javascript.
Any idea how I can acplish this?
Thanks.
I'm doing some caching via javascript. I have a method that takes an array of strings and returns a processed result. I want to create a unique ID from these strings and then use that as the key in an object to store the result. This way the keys in the cache take up as little memory as possible.
In essence I want something like SHA1, but for javascript.
Any idea how I can acplish this?
Thanks.
Share Improve this question edited Mar 14, 2011 at 1:46 Ken Redler 23.9k8 gold badges59 silver badges69 bronze badges asked Mar 13, 2011 at 23:14 Binary LogicBinary Logic 2,6027 gold badges32 silver badges41 bronze badges5 Answers
Reset to default 3Unfortunately, there's no way to get 100% guaranteed uniqueness without using the entire contents of the array as your key. Most good, non-cryptographic hashes will only reduce collisions to an amount that's acceptable for good performance in a hash table, but you still need to verify that the entire contents match.
Even a cryptographic hash like SHA-1 or MD5 can still have collisions, but it's extremely unlikely in most cases. If that's good enough, I would probably go with SHA-1. Otherwise, I would convert the array to a string to use as your key and let JavaScript worry about hashing and collisions.
In any case, you're probably trading performance (the native hashing that JavaScript does is likely to be much faster than anything you can write in JavaScript) and possibly absolute correctness for space.
Also, whether you do the hashing yourself, or let JavaScript do it, be careful about how you convert the array into a string because simple concatenation may not be unique (even with a separator).
Depending on the nature of the values in the arrays, you might be able to cook up something fast and suitable for your case. It's also important to think about what the chances of a collision are and what are its consequences. Since we don't currently have all this information, I can only provide some starting points to work from:
- If the concatenation of the strings is expected to be "long", you will want to use some kind of "hash" that returns a shorter value.
- You probably don't need a crypto-strength hash, so md5 or sha1 is probably overkill
- Even low-tech, fast hashes like
(length of string concat as int) + '/' + (number of strings as int) + '/' + (first char of each string)
may be fine depending on your expected values
Finally, here's an implementation of string.GetHashCode()
ported from C#. If it's good enough for .NET, it's probably good enough for you.
var str = "concatenation of all array values";
var hash1 = (5381<<16) + 5381;
var hash2 = hash1;
var hashPos = 0;
while(hashPos < str.length) {
hash1 = ((hash1 << 5) + hash1 + (hash1 >> 27)) ^ str.charCodeAt(hashPos);
if( hashPos == str.length - 1) {
break;
}
hash2 = ((hash2 << 5) + hash2 + (hash2 >> 27)) ^ str.charCodeAt(hashPos + 1);
hashPos += 2;
}
return hash1 + (hash2 * 1566083941);
Without using a hash, you won't get something unique and small.
Doing myArray.join()
may guarantee unique, but could consume a large amount of memory and run into edge cases where it won't be unique.
Best best is to use an implementation of a hashing algorithm in JavaScript.
You want sha1 in JavaScript? Here -> http://pajhome.uk/crypt/md5/sha1.html
Maybe this :
var newDate = new Date;
var uid = newDate.getTime();
or this :
var uid = Math.random() * Math.pow(10, 17) + Math.random() * Math.pow(10, 17) + Math.random() * Math.pow(10, 17) + Math.random() * Math.pow(10, 17));
There are many ways of getting something as close as an unique id, and since you're working with javascript for caching purposes, it gets easier. It's a matter of choosing what fits you best.
本文标签: uniqueidentifiercreating a unique ID from an array of strings in javascriptStack Overflow
版权声明:本文标题:uniqueidentifier - creating a unique ID from an array of strings in javascript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744795856a2625570.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论