[英]Why is for (var i = 100; i--;) {} so much slower (70%) than for (var i = 100; i-->0;) {} in Firefox?
Here's the test: http://jsperf.com/forloopspeed这是测试: http://jsperf.com/forloopspeed
As you can see, the difference is huge in Firefox, present to a much lesser extent in Safari, and absent in Chrome and Opera.如您所见,Firefox 中的差异很大,在 Safari 中存在的程度要小得多,而在 Chrome 和 Opera 中则没有。
The analogous thing happens with while loops too: http://jsperf.com/whileloopspeed while 循环也会发生类似的事情: http://jsperf.com/whileloopspeed
My guess is that checking whether i
(a Number) is a falsy value is more computationally expensive than checking true
/ false
(the result of the comparison).我的猜测是,检查
i
(一个数字)是否是一个虚假值比检查true
/ false
(比较的结果)在计算上更昂贵。
I suppose the internal ToBoolean()
that is performed on the result of the expression is a bit slower when being given a number
as compared to being given a boolean
.我认为,与给定
boolean
相比,在给定number
时,对表达式结果执行的内部ToBoolean()
会慢一些。
In this test I get a difference in performance when converting to boolean from a boolean vs a number using !!
在这个测试中,当我从 boolean 转换为 boolean 与使用
!!
. .
This looks like some issue specific to Jaegermonkey.这看起来像是 Jaegermonkey 特有的一些问题。 If I run the test under Tracemonkey, the effect disappears.
如果我在 Tracemonkey 下运行测试,效果就消失了。
Filed https://bugzilla.mozilla.org/show_bug.cgi?id=670493归档https://bugzilla.mozilla.org/show_bug.cgi?id=670493
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.