Two days ago, when I found jsperf.com which has a collection of many javascript performance tests, I browsed several tests.
One of tests was this, which compares Math.min(a,b)
vs a<b?a:b
. When I ran this test on Google Chrome, it turned out that a<b?a:b
is much faster than Math.min(a,b)
(On Chrome 14, first one is 53,661,381 ops/s
and second one is 419,830,711 ops/s
). Other web browsers have similar results.
However, on firefox, the result is opposite. Math.min(a,b)
is much faster than a<b?a:b
! First one is 374,219,869 ops/s
and second one is 79,490,749 ops/s
on Firefox 6.
When I posted this on Facebook, someone said that "Since Firefox is open source project, developers optimized Math.min
, but Google Chrome didn't, since Google Chrome is just a modification of Chromium", but (beside that above statement is not quite right) that makes no sense, because that doesn't explain the reason why Google Chrome's a<b?a:b
and Firefox's Math.min(a,b)
performs in similar speed, and Google Chrome's Math.min(a,b)
and Firefox's a<b?a:b
performs in same speed, because if Firefox is faster than Google Chrome, then Google Chrome's Math.min(a,b)
should be much slower than Firefox's a<b?a:b
.
Summary :
- On the other browsers,
a<b?a:b
is faster thanMath.min(a,b)
. - However, on Firefox,
Math.min(a,b)
is faster thana<b?a:b
. - Since the speed of
Math.min(a,b)
on Firefox ≒ the speed ofa<b?a:b
on Google Chrome and speed ofa<b?a:b
on Firefox ≒ the speed ofMath.min(a,b)
on Google Chrome, "Firefox is slow" or "Firefox is fast" can't be a reason.
Is there any reason why (how) this happens?