三元运算符,if-else 或逻辑 OR 在 javascript 中是否更快?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2586842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is ternary operator, if-else or logical OR faster in javascript?
提问by Jeff
Which method is faster or more responsive in javascript, if-else, the ternary operator or logical OR? Which is advisable to use, for what reasons?
在 javascript、if-else、三元运算符或逻辑 OR 中,哪种方法更快或更响应?哪个是可取的,出于什么原因?
采纳答案by Andrew Hare
The speed difference will be negligible - use whichever you find to be more readable. In other words I highly doubt that a bottleneck in your code will be due to using the wrong conditional construct.
速度差异可以忽略不计 - 使用您认为更具可读性的任何一个。换句话说,我非常怀疑代码中的瓶颈是由于使用了错误的条件构造。
回答by charlie roberts
Seems like nobody did any actual profiling. Here's the code I used:
似乎没有人做过任何实际的分析。这是我使用的代码:
test = function() {
for (var i = 0; i < 10000000; i++) {
var a = i < 100 ? 1 : 2;
/*
if(i < 100) {
var a = 1;
}else{
var a = 2;
}
*/
}
}
test();
Using the if/elseblock instead of the ternary operator yields a 1.5- 2xperformance increase in Google Chrome v21under OS X Snow Leopard.
使用if/else块,而不是三元运算符的产生一个1.5- 2倍的性能在提高谷歌浏览器V21下OS X雪豹。
As one use case where this is veryimportant, synthesizing real-time audio is becoming more and more common with JavaScript. This type of performance difference is a big deal when an algorithm is running 44100 times a second.
作为一个非常重要的用例,使用 JavaScript 合成实时音频变得越来越普遍。当算法每秒运行 44100 次时,这种类型的性能差异是一个大问题。
回答by Andrew Luhring
I didn't think @charlie robert's test was fair
我不认为@charlie robert 的测试是公平的
Here's my jsperf
这是我的jsperf
result:
结果:
- strict equal is the fastest
- strict ternary is 33% slower
- truthy falsy is 49% slower
- ternary truthy falsy is 55% slower
- if else and ternary are roughly the same.
- 严格相等是最快的
- 严格的三进制慢 33%
- 真假慢 49%
- 三元 truey falsy 慢 55%
- if else 和 ternary 大致相同。
normal equal and normal ternary slowest.
正常相等和正常三元最慢。
strict equals:
严格等于:
var a = true, b;
if (a === true) {
b = true;
} else {
b = false
}
if (a === false) {
b = true;
} else {
b = false;
}
ternary strict equals
三元严格等于
var a = true, b;
b = (a === true) ? true : false;
b = (a === false) ? true : false;
simple equality
简单的平等
var a = true, b;
if (a == true) {
b = true;
} else {
b = false;
}
if (a == false) {
b = true;
} else {
b = false;
}
simple ternary equality
简单三元等式
var a = true, b;
b = (a == true) ? true : false;
b = (a == false) ? true : false;
truthy / falsy
真/假
var a = true, b;
if (a) {
b = true;
} else {
b = false;
}
if (!a) {
b = true;
} else {
b = false;
}
ternary truthy / falsy
三元真/假
var a = true, b;
b = (a) ? true : false;
b = (!a) ? true : false;
回答by Jay Edwards
to charlie roberts' answer above, I would add:
对于上面查理罗伯茨的回答,我要补充说:
the following link gives some incisive answers; the result for switches in Firefox being the most striking: http://jsperf.com/if-else-vs-arrays-vs-switch-vs-ternary/39
以下链接给出了一些精辟的答案;Firefox 中开关的结果最引人注目:http: //jsperf.com/if-else-vs-arrays-vs-switch-vs-ternary/39
Those who question why anyone would investigate optimization to this degree would do well to research WebGL!
那些质疑为什么有人会研究这种程度的优化的人最好研究 WebGL!
回答by cwalsh003
Ternary operator is only syntactic sugar, not a performance booster.
三元运算符只是语法糖,而不是性能助推器。
回答by Delbo ar
I'm doing another syntax:
我正在做另一种语法:
var a = true,
b;
b = (a == false)
? true // if a == false, b = true
: false; // else: a == true so b = false
/* Equivalent of
if(a == true)
var b = true;
else
var b = false;
Some people like my code and tell me it's simple to read.
有些人喜欢我的代码并告诉我它很容易阅读。
回答by Erix
There is no difference in speed.
速度没有区别。
Some prefer the if/else for readability. Personally, I use the ternary operator whenever the logic is trivial enough to understand on one line.
有些人更喜欢 if/else 以提高可读性。就个人而言,只要逻辑简单到可以在一行上理解,我就会使用三元运算符。

