@Cristian Traìna Node 不允许跳过空循环。这些优化只允许在编译语言(如 Pascal 或 C/C++)中使用-O2
.
对于这个程序
var max=process.argv[2];
for(var i=0;i<=max;i++){} // with let results is the same
我们可以从许多循环中获得以下执行时间的依赖性。它是一个 LogLog 图表。它支配部分执行时间的第一个平坦区域是 NodeJs 的启动。在 1M 循环之后,您可以看到迭代次数随时间线性增加。最高测量大约需要 1000 秒,因此 V8 不会跳过空循环。

(来源:gustawdaniel.pl)
从问题回到脚本:
function-call: 154.878662109375ms
in-function: 153.7490234375ms
function-call: 154.183ms
in-function: 152.907ms
function-call: 156 ms debugger eval code:4:3
in-function: 1519 ms
function-call: 158.954345703125ms
in-function: 153.663818359375ms
function-call: 153.548095703125ms
in-function: 153.755126953125ms
function-call: 154.34814453125ms
in-function: 154.729248046875ms
我朋友的电脑测试:
function-call: 3 496,6 ms
in-function: 2 330,9 ms
function-call: 70.69580078125ms
in-function: 70.43310546875ms
所以现在这似乎是 Firefox 和 Edge 的问题。
除了 Firefox 和 Edge 之外,这些浏览器中的任何一个都使用 V8 引擎。火狐在这里描述:
https://www.digitaltrends.com/web/mozilla-firefox-new-browser-engine-quantum-2017/
并使用Quantum Flow:
.
Edge 团队考虑采用 Chromium 引擎
https://www.pcmag.com/news/365345/microsofts-edge-browser-to-adopt-googles-chromium-engine
从这篇文章
https://blog.mozilla.org/blog/2018/12/06/goodbye-edge/
我们只能看到这一点Chromium from Google
,Gecko Quantum from Mozilla
并将在未来得到支持。
如果有人可以访问 Safari 或 Edge,请附加测试。