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

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

问题描述

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

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
- 使用in"运算符
- 测试对象的属性值

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

我们看到数组因大"键计数而失败.
所以你必须弄清楚你所处的情况.
通过少量的键计数,处理属性的开销使数组获胜.
键数很大,数组内迭代的代价使得属性取胜...

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天全站免登陆