简体   繁体   English

在javascript中从字符串数组创建唯一ID?

[英]creating a unique ID from an array of strings in javascript?

I'm doing some caching via javascript. 我正在通过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. 我想从这些字符串创建一个唯一的ID,然后将其用作对象中的键来存储结果。 This way the keys in the cache take up as little memory as possible. 这样,缓存中的密钥占用尽可能少的内存。

In essence I want something like SHA1, but for javascript. 本质上我想要像SHA1这样的东西,但是对于javascript。

Any idea how I can accomplish this? 知道我怎么能做到这一点?

Thanks. 谢谢。

Unfortunately, there's no way to get 100% guaranteed uniqueness without using the entire contents of the array as your key. 不幸的是,如果不使用数组的全部内容作为密钥,就无法获得100%保证唯一性。 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. 即使是像SHA-1或MD5这样的加密哈希仍然会发生冲突,但在大多数情况下这种情况极不可能。 If that's good enough, I would probably go with SHA-1. 如果那还不错,我可能会选择SHA-1。 Otherwise, I would convert the array to a string to use as your key and let JavaScript worry about hashing and collisions. 否则,我会将数组转换为字符串以用作密钥,让JavaScript担心散列和冲突。

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. 在任何情况下,你可能交易表现(本地哈希的JavaScript确实是可能比任何你能在JavaScript写快得多 ),以及可能的绝对正确性空间。

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). 此外,无论您是自己进行散列,还是让JavaScript进行散列,请注意如何将数组转换为字符串,因为简单的连接可能不是唯一的(即使使用分隔符)。

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. 执行myArray.join() 可能会保证唯一,但可能会消耗大量内存并遇到不具有唯一性的边缘情况。

Best best is to use an implementation of a hashing algorithm in JavaScript. 最好的方法是在JavaScript中使用散列算法的实现。

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: 由于我们目前没有所有这些信息,我只能提供一些起点来工作:

  1. 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. 如果字符串的串联期望是“长”,那么您将需要使用某种返回较短值的“哈希”。
  2. You probably don't need a crypto-strength hash, so md5 or sha1 is probably overkill 你可能不需要加密强度哈希,所以md5或sha1可能是矫枉过正
  3. 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 即使是低技术,快速哈希,如(length of string concat as int) + '/' + (number of strings as int) + '/' + (first char of each string)可能没问题,具体取决于您的预期值

Finally, here's an implementation of string.GetHashCode() ported from C#. 最后,这是从C#移植的string.GetHashCode()的实现。 If it's good enough for .NET, it's probably good enough for you. 如果它对.NET来说足够好,那对你来说可能已经足够了。

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);

You want sha1 in JavaScript? 你想在JavaScript中使用sha1吗? Here -> http://pajhome.org.uk/crypt/md5/sha1.html 这里 - > http://pajhome.org.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. 有很多方法可以获得与唯一ID一样接近的内容,并且由于您正在使用javascript进行缓存,因此它变得更容易。 It's a matter of choosing what fits you best. 这是一个选择最适合你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM