简体   繁体   English

n = n +1比n ++或++ n快,为什么?

[英]n = n +1 is faster than n++ or ++n, why?

Check this test case: http://jsperf.com/nn-1-or-n 检查此测试用例: http//jsperf.com/nn-1-or-n

n = n + 1;

is faster than 比...更快

n++;

and

++n;

Any clue about why the first writing is so much faster on many browsers ? 关于为什么第一次写作在许多浏览器上如此快得多的任何线索?

The performance will differ by browser and computer. 浏览器和计算机的性能会有所不同。

I see that n = n + 1 on my setup is about 4 times faster. 我看到我的设置n = n + 1大约快4倍。

At the same time, the slowest is over 62 million ops per second. 与此同时,最慢的是每秒超过6200 万次操作。

You are micro-optimizing here. 你在这里进行微观优化。 The usage of one over another is hardly going to be a bottleneck. 一个在另一个上面的使用几乎不会成为瓶颈。


Why it is faster in some browsers? 为什么在某些浏览器中速度更快? I don't know. 我不知道。 You would need to dig into the source code of the different JavaScript engines to find out. 您需要深入了解不同JavaScript引擎的源代码才能找到答案。

Chances are good that there is an optimization for this case due to how some popular micro-benchmarks are written. 由于一些流行的微基准测试的编写方式,这种情况有一个优化的机会很好。

Check this test. 检查此测试。 Or here is another test. 或者是另一个测试。 预先测试

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

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