Are local variables faster? Part 2
As a follow-up to my last post, I’ve devised a new experiment to test the relative speed of accessing global variables versus accessing local variables. Several commenters correctly pointed out that my first experiment was flawed due to the repeated variable initialization in the local variable test case. I clearly forgot to take into account variable initialization time. Special thanks to Dmitry Monin and Matt Sweeney for devising different experiments that proved this flaw.
Still, with this new experiment, I’m not seeing the dramatic speed difference I expected. In most browsers, the results tend to favor local variables slightly:
- Safari 3.0.3 (Windows) shows that roughly 10% of the time, global variables are either faster or equal to local variables.
- Opera 9.02 shows a similar occurrence, with global variables appearing faster or equal to local variables at around 15% of the time.
- Internet Explorer 7 shows global variables are faster than, or equal to, local variables about 10% of the time as well.
- Firefox 126.96.36.199 is the most consistent browser, with local variables repeatedly beating up on global variables with only a small incidence of equality or the inverse relationship (less than 5%).
With this revised experiment, I feel comfortable saying that local variables are faster than global variables across the board…but not by much. Firefox shows the greatest variance between performance of globals and locals, but even so, the performance isn’t all that bad. If you’re counting every millisecond, it may make a difference, but chances are that you’ll get more bang for your development buck by reducing DOM interactions and using event delegation rather than refactoring code to use local variables.
Disclaimer: Any viewpoints and opinions expressed in this article are those of Nicholas C. Zakas and do not, in any way, reflect those of my employer, my colleagues, Wrox Publishing, O'Reilly Publishing, or anyone else. I speak only for myself, not for them.
Both comments and pings are currently closed.