jquery.inArray() 与 Object.hasOwnProperty() 之间的性能差异?
我有一种情况,我可以选择将字符串键的集合实现为对象:
$.each(objects, function (key, object) {
collection[key] = "doesn't matter";
});
或数组:
$.each(objects, function (key, object) {
collection.push(key);
});
我希望能够快速确定该集合是否包含给定的键。如果集合是一个对象,我可以使用:
if (collection.hasOwnProperty(key_to_find)) { // found it!... }
else { // didn't find it... }
如果集合是一个数组,我可以使用:
if ($.inArray(key_to_find, collection)) { // found it!... }
else { // didn't find it... }
我想使用 JavaScript 的内置 hasOwnProperty 会比 jQuery 的 inArray() 更快,但我不完全确定。有谁知道更多关于这两种方法之间的性能差异?或者,这里有我不知道的更有效的替代方案吗?
I have a situation where I can choose to implement a collection of string keys as an object:
$.each(objects, function (key, object) {
collection[key] = "doesn't matter";
});
or an array:
$.each(objects, function (key, object) {
collection.push(key);
});
I'd like to be able to quickly determine whether or not the collection contains a given key. If collection is an object, I can use:
if (collection.hasOwnProperty(key_to_find)) { // found it!... }
else { // didn't find it... }
If collection is an array, I can use:
if ($.inArray(key_to_find, collection)) { // found it!... }
else { // didn't find it... }
I'd imagine using JavaScript's built-in hasOwnProperty would be faster than jQuery's inArray(), but I'm not entirely sure. Does anyone know more about the performance differences between these two methods? Or, is there a more efficient alternative here that I am not aware of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果我们讨论的是检查需要多长时间,那么实际上没有争议:
http://jsperf. com/array-vs-obj
hasOwnProperty 由于其他人所述的原因要快得多。
If we're talking just how long it takes to check, then there's really no contest:
http://jsperf.com/array-vs-obj
hasOwnProperty is way way faster for the reasons stated by others.
嗯,如果集合是一个数组,您还可以在其上使用本机 indexOf,不是吗? https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects /数组/indexOf
Mmmh, if the collection is an array you can also use the native indexOf on it, no? https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
数组方法速度较慢,因为它需要线性时间来查找数组中的元素(代码必须逐步遍历可能的每个元素)。 hasOwnProperty 会快得多,因为它可以使用哈希表查找,这会在恒定时间内发生。
The array method is slower, because it requires linear time to find an element in the array (the code must step through potentially every element). hasOwnProperty will be much faster as it can use a hash table lookup, which occurs in constant time.
对象的属性访问时间非常快。但是,您仍然需要考虑计算哈希等的开销(如果实现使用哈希表来支持对象)。如果键集相对较小,则不应有太大差异。如果它更大,那么我会使用对象/哈希来存储属性。
也就是说,使用对象管理重复键要容易一些,所以我个人会使用字典。
除非这是您的应用程序中的瓶颈,否则您不应该过度考虑它。
An object will have very fast access times for properties. However, you still have to account for overhead from calculating the hash etc (if the implementation uses hash tables to back objects). If the set of keys is relatively small, there shouldn't be too much of a difference. If it's larger, then I would go with the object/hash to store properties.
That said, it's a little easier to manage duplicate keys with the object, so I would personally would go with the dictionary.
Unless this is a bottleneck in your application, you shouldn't over think it.
简短答案: .indexOf() (如 @Claudio 提到的)
长答案: 看看我编写的快速速度测试 - http://jsfiddle.net/cvallance/4YdxJ/3/ - 差异确实可以忽略不计。
Short Answer: .indexOf() (as @Claudio mentions)
Long Answer: Have a look at a quick speed test I coded up - http://jsfiddle.net/cvallance/4YdxJ/3/ - the difference is really negligible.