简体   繁体   English

三元语句比javascript中的if / then / else语句快吗?

[英]Are ternary statements faster than if/then/else statements in javascript?

I see a lot of: 我看到很多:

var something = (is_something_true()) ? 3 : 4;

in javascript. 在javascript中。 Is this faster than 这比

var something;
if (is_something_true()) {
    something = 3;
} else {
    something = 4;
}

Or is it written concisely for convenience? 还是为了方便起见简洁地编写了它?

Please enjoy this -- if difference is statistically valid then the result (true or false) also matters -- clearly this is just other stuff on the machine having an impact on browser performance: 请享受这一点-如果差异在统计上是有效的,那么结果(对或错)也很重要-显然,这只是机器上影响浏览器性能的其他因素:

Here is the link 链接在这里

结果不同!

There is a fundamental difference between the two, the ternary statements are expressions and not flow of control. 两者之间有根本的区别,三元语句是表达式而不是控制流。 If there is a case where someone writes it as a ternary expression instead of a standard if / than / else, when both would work the same they are (in my opinion) making the code harder to read without good reason. 如果存在某人将其写为三元表达式而不是标准if / else /的情况,那么当两者都可以工作时(我认为),它们会使代码在没有充分理由的情况下难以阅读。

In terms of speed there should be no difference. 在速度方面,应该没有差异。 Unless you are using a really bad javascript implementation. 除非您使用的是非常糟糕的javascript实现。 The slowest part of both statements is the branching. 这两个语句中最慢的部分是分支。

You should write for readability first and tiny micro-optimizations one-hundred and fifty-second. 您应该首先为提高可读性而写,然后再进行一百五十秒的微小优化。 The first form is easier to read in many cases and there probably isn't much of a difference in performance one way or the other. 在许多情况下,第一种形式更易于阅读,并且一种或另一种方式的性能可能没有太大区别。

(Even if you disagree and think the second form is easier to read, asking about the relative performance difference is still the wrong question.) (即使您不同意并认为第二种形式更易于阅读,询问相对性能差异仍然是错误的问题。)

Here is the statisitics : 这是统计数据 在此处输入图片说明

After multiple tests and observations, it can be concluded,that the most cases the ternary operator( ?: ) is slower,than if/else . 经过多次测试和观察,可以得出结论,在大多数情况下,三元运算符( ?: :)比if/else慢。

Yes, there is negligible difference between the two. 是的,两者之间的差异可以忽略不计。

However the difference is so small that it doesn't matter which one you use (I prefer if/else) because they help in readability which would save you a lot of time if someone is going through your code or maybe you yourself are maybe after say 3 months or so. 但是,差异是如此之小,以至于您使用哪一个都没关系(我更喜欢if / else),因为它们有助于提高可读性,如果有人正在遍历您的代码,或者您也许自己在追查代码,那么它们将为您节省大量时间说3个月左右。

For those people who want to check the difference try this code: 对于那些想要检查差异的人,请尝试以下代码:

// declarations  
var num1 = 10, num2, i = 0, startTime, endTime, x, y;

// start timer
startTime = Math.floor((new Date()).getTime());

for(; i < 1e8; i++) {
  // first part if /else
  if(x == 10)
    y = x;
  else
    y = 0;

  // second part ternary
  y = (x == 10) ? x : 0;
}

// end timer     
endTime = Math.floor((new Date()).getTime() - startTime);
document.write("Time taken " + endTime + " ms");  

Note: Comment one of the part and execute the code and run the loop for large number of iterations (above code millions of iterations). 注意:注释其中一部分,然后执行代码并运行循环进行大量迭代(上面的代码执行了数百万次迭代)。

Tip: Try running the loop multiple times to get average. 提示:尝试多次运行循环以获得平均值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM