OR运算(||)与inArray()的性能 [英] Performance of OR operation ( || ) vs inArray()

查看:126
本文介绍了OR运算(||)与inArray()的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您要检查用户在表单字段中输入的输入字符串。根据可能值列表检查此输入的最快方法是哪一种?

Suppose that you want to check what input string a user has entered in a form field. Which one would be the fastest way to check this input against a list of possible values?

以下示例使用 jQuery

第一种方法:使用 ||

if (input == "firstValue" || input == "secondValue" || ... ) {
    ...
}

第二种方法:使用 inArray()

if ($.inArray(input, array) >= 0) {
    ...
}

这两种方法之间是否存在显着差异?

Are there any significant differences between these two methods?

推荐答案

您不希望以最快但最易读的方式。那是 in_array()(JavaScript: array.indexOf(value)> = 0 )超过2或者3个值。

You don't want the fastest but the most readable way. And that's in_array() (JavaScript: array.indexOf(value) >= 0) for more than 2 or 3 values.

性能差异可以忽略不计 - 虽然函数调用和数组创建肯定会有一些开销,但与昂贵的操作(如文件访问)相比无关紧要,数据库访问,网络访问等。所以最后没有人会注意到差异。

The performance difference is negligible - while a function call and array creation certainly has some overhead, it doesn't matter compared to expensive operations such a file access, database access, network access, etc.. So in the end nobody will notice the difference.

这是一个简短的基准,每个都有100万次迭代:

Here's a short benchmark, each with 1 million iterations:

5.4829950332642 - in_array, the array is recreated everytime
2.9785749912262 - in_array, the array is created only once
0.64996600151062 - isset(), the array way created only once and then the values were turned to keys using array_flip()
2.0508298873901 - ||

所以,最快,但仍然非常易读的方法就是这样。除非您只创建一次 $ arr 并使用多次次,否则不需要这样做,您只需使用 in_array()

So, the fastest, yet still very readable way is this. Unless you create $arr only once and use it many times, there is no need for this and you can simply stay with in_array().

$arr = array_flip(array('your', 'list', 'of', 'values'));
if(isset($arr[$value])) ...






如果您 要求使用JavaScript(在这种情况下摆脱那些 $ 前缀! ),最好的解决方案是使用 Array.indexOf()


In case you did ask for JavaScript (in this case get rid of those $ prefixes!), the best solution is using Array.indexOf():

['a', 'b', 'c'].indexOf(value) >= 0

但是,并非所有浏览器都支持 Array.indexOf(),所以你可能想要使用例如来自 Underscore.js 的功能:

However, not all browsers already support Array.indexOf(), so you might want to use e.g. the function from Underscore.js:

_.contains(['a', 'b', 'c'], value)

jQuery也有这样的功能:

jQuery also has a function for this:

$.inArray(value, ['a', 'b', 'c'])

最快的方法是使用一个对象和运算符中,但对象定义的可读性低于数组定义:

The fastest way would be with an object and the in operator, but the object definition is less readable than the array definition:

value in {'a':0, 'b':0, 'c':0}

这是针对各种解决方案的JSPerf基准: http://jsperf.com/inarray-vs-or - 但是,在大多数情况下,相当大的性能差异可以忽略不计,因为您不会在循环中执行数百万次代码。

Here's a JSPerf benchmark for the various solutions: http://jsperf.com/inarray-vs-or - but again, the rather big performance difference is negligible in most cases since you are not going to execute the code millions of times in a loop.

这篇关于OR运算(||)与inArray()的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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