IE operating faster with function calls?
Asked Answered
P

1

7

Looking for ways to optomize my code, I happened upon this jsPerf test. Not expecting anything other than to have my notion of the slowness of function calls reaffirmed, my results with IE 9 really threw me for a loop. Code which utilized function calls was faster, but only on this one browser. I ran it multiple times with the same result. I can't see that the test was set up incorrectly. What could be causing this strange result?

My user agent is Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0 running on Windows Server 2008.

Pearly answered 19/12, 2012 at 14:49 Comment(4)
Odd, IE 10 doesn't do that either.Stifle
Just confirmed that it's only an IE 9 thing. Tried it again on another computer.Pearly
Was the tab process 32bit or 64bit? Was script debugging enabled or disabled? Is there a difference if you ever use the result of the test (e.g. preventing optimizing out the entire thing)Thiel
it seems that IE9 does the exception, so weird in all cases logically inline should be faster or equal to function calls, the list is getting longer, and one issue with safari once I did the test it did not include my test in the chart.Odele
I
4

Disclaimer: I’m the creator of jsPerf.com.


Your first test is the following:

var i = 0;
for (i = 0; i < 1000; i++) {
  test()
}

Why include the for loop there? It only skews the result. jsPerf automatically repeats the test code until it performed enough tests to achieve a statistically significant result. Ideally, jsPerf tests are as compact as possible, and only test what you really want to test. In this case, you’re not interested in for loop performance at all — you just want to find out if inlining code is faster than calling a function or not.

If you’re interested in other tips on creating robust jsPerf test cases, check out my #jsconfeu2011 presentation.

Note: I’m not saying the redundant for loop is the reason why you’re seeing this result. It might be a factor, but there may be something else that further skews the result. This might be IE9’s “dead code removal” feature kicking in.

Anyway, I’ve forked your jsPerf test, removed the loops, and made the variables global in an attempt to avoid dead code elimination optimizations. http://jsperf.com/function-calls-vs-inline/3 Could you test this in IE9? I don’t have an IE9 VM handy at the moment.

Immobilize answered 13/7, 2013 at 19:33 Comment(2)
I don't have access to my machine with IE9 right now, but perhaps even more strangely, Chrome is now operating faster with function calls as well.Pearly
@TreyKeown: Interesting, I can reproduce the results in Chrome 28, but not in Chrome 30 Canary. It might have been a fluke in the v8 version that shipped with Chrome 28. Something similar happened a while back — at some point, performing a scope lookup was faster than not performing a scope lookup in v8: jsperf.com/scope-lookupsImmobilize

© 2022 - 2024 — McMap. All rights reserved.