Reputation: 2592
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 accomplish this?
Thanks.
Upvotes: 3
Views: 4227
Reputation: 490283
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.
Upvotes: 1
Reputation: 102725
Unfortunately, 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).
Upvotes: 3
Reputation: 437396
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:
(length of string concat as int) + '/' + (number of strings as int) + '/' + (first char of each string)
may be fine depending on your expected valuesFinally, 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);
Upvotes: 1
Reputation: 10981
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.
Upvotes: -1
Reputation: 70523
You want sha1 in JavaScript? Here -> http://pajhome.org.uk/crypt/md5/sha1.html
Upvotes: 0