Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE operating faster with function calls?

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.

like image 294
Trey Keown Avatar asked Dec 19 '12 14:12

Trey Keown


1 Answers

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.

like image 89
Mathias Bynens Avatar answered Sep 27 '22 20:09

Mathias Bynens