检查对象的键是否比在数组中搜索字符串更有效? [英] Is checking an object for a key more efficient than searching an array for a string?

查看:149
本文介绍了检查对象的键是否比在数组中搜索字符串更有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个数据结构,其中包含我将用来反复检查以查看是否定义了某些值的数据结构。我想出了两个可能的解决方案,我想知道哪个更有效,或者是否有更好的方法:



1)使用数组: keys = ['key1','key2','key3']



我可以像这样创建一个数组然后使用 jQuery.inArray(keyToCheck,keys)> -1 检查并查看 keyToCheck 是否在我的数组中。



2)使用对象: keys = {key1:1,key2:1,key3:1}



我可以创建这个对象然后使用 keys [keyToCheck] || 0 查看 keyToCheck 是否已定义。



我不确定是如何在javascript中实现搜索对象,以及它是否比 jQuery.inArray 更有效,它循环遍历数组。这些方法之间是否存在性能差异?使用jQuery对我来说不是问题,因为我已经在我的代码中出于其他原因。

解决方案

当想知道表演时,获得答案的方法是在jsperf上测试几个案例。

(可能还有其他基准站点我不知道,如果你知道另一个,请评论,我不是故意的广告)



对于你的情况,我测试了3种方法:

- 在数组中使用indexOf

- 使用'在'operator

- 测试对象的属性值



psperf在这里大约有10个项目在这里:



我们看到阵列因大键数而失败。

所以你必须明确你所处的情况。

使用一点键计数,处理属性的开销使得数组获胜。

使用大的键计数,在数组中迭代的成本使得属性获胜...


I'm creating a data structure that will contain that I will be using to repeatedly check to see if certain values are defined. I've come up with two possible solutions, and am wondering which is more efficient, or if there is a better way than either:

1) Use an array: keys = ['key1' , 'key2', 'key3']

I can create an array like this and then use jQuery.inArray(keyToCheck, keys) > -1 to check and see if keyToCheck is in my array.

2) Use an object: keys = {key1 : 1, key2 : 1, key3: 1}

I can create this object and then use keys[keyToCheck] || 0 to see if keyToCheck is defined.

What I'm not sure about is how searching an object is implemented in javascript, and whether or not it's more efficient than jQuery.inArray, which loops through an array. Is there a performance difference between these methods? Using jQuery isn't an issue for me since I already have it in my code for other reasons.

解决方案

When wondering about performances, a way to get an answer is to test several cases on jsperf.
(There might be other benchmark site i don't know of, please comment if you know another one, i don't mean to advertise)

For your case, i tested 3 methods :
- using indexOf in an array
- using the 'in' operator
- testing the object's property value

the psperf is here for around 10 items is here : http://jsperf.com/key-or-array-search/2

We can see that using the object's property value is by far faster on Firefox (>20 times faster than array, >5 times than in).
On Safari, everything is slower than on Firefox, still the object property access is more than two times faster.
But on Chrome i don't understand what's happening : all 3 methods are quite close, but the fastest is the array/indexOf method, ...

Performances are often surprising.

Notice that results might change -even change dramatically- depending on the number of keys (5, 20, 5000 ?), and also on the probability that a checked key is within the set.

I wondered how things would change wit a 500 length key array. Here are the results : http://jsperf.com/key-or-array-search/3

We see that the array is defeated for a 'big' key count.
So you have to make clear the situation you're in.
With a little key count, the overhead of handling a property makes the array win.
With a large key count, the cost of iterating within the array makes the property win...

这篇关于检查对象的键是否比在数组中搜索字符串更有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆