简体   繁体   English

JavaScript中“>”和“!=”之间是否存在性能差异?

[英]Is there a performance difference in JavaScript between “>” and “!=”?

Doing a bind for a previous link in a nav. 对导航中的上一个链接进行绑定。 If we're on the first position don't do the same thing for the previous link. 如果我们位于第一个位置,请不要对上一个链接执行相同的操作。 I was doing a "!=" not equal to test, but realized it could be a ">" greater than. 我正在做一个“!=”不等于测试,但意识到它可能大于“>。

Then I thought, is one faster? 然后我想,是更快吗?

if (numberToCheck != 0) {
    //doSomething();
}

vs.

if (numberToCheck > 0) {
    //doSomething();
}

Performance questions should be resolved via measurement, not speculation. 绩效问题应通过评估而非猜测来解决。

You can see for yourself here http://jsperf.com/inequality-vs-greater-than . 您可以在http://jsperf.com/inequality-vs-greater-than上亲自查看。 The results of this test (on my computer) vary by browser. 此测试的结果(在我的计算机上)因浏览器而异。 Some are faster with inequality. 有些不平等会更快。 Some are faster with less than. 有些比少更快。 You will likely find much bigger speed differences in other areas of your code. 您可能会在代码的其他区域发现更大的速度差异。

If you want to test something slightly different than what I put in the test, just add your own test for comparison. 如果您想测试与我在测试中稍有不同的东西,只需添加自己的测试以进行比较即可。

Do you mean like... absolute difference, or "meaningfully different"? 您是说……绝对不同,还是“意义不同”?

In any case, it would depend 100% on the underlying VM implementation. 无论如何,这将100%取决于基础VM的实现。 It might be faster to set a flag and have that flag be the first in an && (for short-circuiting), though, and have the numerical part second. 但是,设置一个标志并使该标志成为&&的第一个(用于短路)可能更快一些 ,而将数字部分作为第二个。

if (keepChecking && numberToCheck != 0) {
    keepChecking == false;
    // doSomething();
}

Again, VM-dependent, and I can't believe it'd matter a lot either way. 同样,依赖于VM,我无法相信这两种方式都非常重要。

What about - 关于什么 -

if (numberToCheck !== 0) {
//doSomething();
}

Actually, I doubt a human could notice the difference. 实际上,我怀疑人类会注意到这种差异。 Plus, each browser js engine might yield different results. 另外,每个浏览器js引擎可能会产生不同的结果。

I made a test page at jsperf: 我在jsperf上做了一个测试页:

http://jsperf.com/different-from http://jsperf.com/different-from

In Chrome on my MacBook Pro, they are exactly the same. 在我的MacBook Pro上的Chrome中,它们完全相同。

暂无
暂无

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

相关问题 函数初始化位置之间的性能差异[JavaScript] - Performance difference between function initialization locations [JavaScript] JavaScript 中的“let”和“var”之间是否存在性能差异 - Is there a performance difference between 'let' and 'var' in JavaScript JavaScript创建的内联样式和JavaScript创建的样式表之间的性能差异 - Performance difference between JavaScript created inline styles and JavaScript created stylesheets javascript中的动态和直接变量返回之间是否存在一些性能差异? - Is it some performance difference between dynamic and direct variable return in javascript? Javascript / jQuery - height()和.css({'height'})之间是否存在性能差异 - Javascript / jQuery - Is there a performance difference between height() and .css({'height'}) JavaScript扩展和NPAPI插件之间的性能差异 - Difference between JavaScript Extensions and NPAPI plugins with respect to performance 双等于(==)和三等于(===)之间的JavaScript性能差异 - JavaScript performance difference between double equals (==) and triple equals (===) + =,++,+之间的性能差异 - Performance difference between +=, ++, + for.in和.forEach()之间是否存在性能差异? - Is there a performance difference between for…in and .forEach()? javascript 中的“点击”function 与 e.target 之间有区别吗? 性能速度? - Is there a difference between "click" function vs e.target in javascript? Performance speed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM