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.