New issue 1704 by bhar...@google.com: V8 significantly slower than Firefox
and Safari in evalutating Math functions.
http://code.google.com/p/v8/issues/detail?id=1704
Detailed description of the issue.
The V8 engine in Chrome evaluates the following 4 functions significantly
slower (upto 7X) than the Firefox and Safari Javascript engines. Also,
passing the Math.sin as a function argument results in a 3x speedup
compared to using the function inline (f1local vs. f2local).
This bug was reported by my friend Srikumar Subramanian, who encountered
this while writing audio processing code in JS.
https://plus.google.com/102694714835839603248/posts/i9dahgLDs9w
DETAILS
=======
f1 = function(n) { sum = 0; for (i = 0; i < n; ++i) sum += Math.sin(i);
return sum; }
f2 = function(f) { return function(n) { sum = 0; for (i = 0; i < n; ++i)
sum += f(i); return sum; } }
f1local = function(n) { var sum, i; sum = 0; for (i = 0; i < n; ++i) sum +=
Math.sin(i); return sum; }
f2local = function(f) { return function(n) { var sum, i; sum = 0; for (i =
0; i < n; ++i) sum += f(i); return sum; } }
Run on: Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz 4GB RAM Windows Vista
(3 years old)
Function f1(N) where N = 100M
40 sec in Firefox 6.0.2
94 sec in Safari 5.1 (7534.50)
275 sec in Chrome 14.0.835.163 beta-m (6X SLOWER)
Equivalent function f2(Math.sin)(N) where N = 100M
33 sec in Firefox 6.0.2
85 sec in Safari 5.1 (7534.50)
260 sec in Chrome 14.0.835.163 beta-m (7X SLOWER)
Function f1local(N) where N = 100M
23 sec in Firefox 6.0.2
28 sec in Safari 5.1 (7534.50)
45 sec in Chrome 14.0.835.163 beta-m (2X SLOWER)
Function f2local(Math.sin)(N) where N = 100M
17 sec in Firefox 6.0.2
13 sec in Safari 5.1 (7534.50)
15 sec in Chrome 14.0.835.163 beta-m
Bharat
Presumably the problem is still there if you feed floating point numbers
into sin() rather than integers? I would suppose that in real life 0 is
the only integer that it makes sense to take the sin of.
The problem is not because we are feeding floating point numbers into
sin(). If you replace Math.sin(i) with Math.min(i,i) you see a similar
performance problem.
The title should be 'V8 significantly slower than Firefox and Safari in
accessing globals'
The V8 times are proportional to the total number of global accesses (Math,
sum, i) which indicates that accessing globals is so slow as to dominate
the time.
The difference between f1 and f1local is that f1 uses global sum and i.
The difference between fl1local and f2local is that f2local
accesses 'Math.sin' once.
The fact that all JS engines are roughly the same for f2local shows that
the calling the function stored at Math.sin is not the issue.
Yes, I agree with your analysis. I don't see a way to update the title.
One note - this speed difference is observable only in the context of a
browser. In Node.js, there appears to be no significant speed difference
between the two functions since there is no global 'window'.
Comment #6 on issue 1704 by fschn...@chromium.org: V8 significantly
slower than Firefox and Safari in evalutating Math functions.
http://code.google.com/p/v8/issues/detail?id=1704
Can you try Chrome 15 or 16 to see if this still reproduces?
I tried reproducing the reported slow-down with Chrome 15.0.874.21 beta and
tip-of-tree 16.0.893.0 (Linux and Windows) and compared with Firefox
nightly 9.0a1 (2011-09-27) and Firefox 7 (Windows), but I could not see any
difference when running f1 with N=100M. These are the number for my Linux
box (Xeon E5...@2.2Ghz)
f1l(N)
Firefox 6.8s
Chrome 15 6.5s
Chrome 16 6.5s
Numbers for Windows show similar trend: Chrome 16 is slightly faster than
Firefox 7.
It could well be that the underlying issue was fixed already with version
15. If you can still reproduce the slowdown, I'll take another look at the
reproduction.
Yes, I can reproduce it with Chrome 15.0.874.21 beta (Xeon(R) 4xCPU X5550 @
2.67GHz, 12GB). f1(100000000) takes 166 sec.
Can you share an html file to reproduce this?