JavaScript Variable Performance
by Nicholas Zakas on Jan 21, 2009
- 8,143 views
My talk at the January 21, 2009 Mountain View JavaScript Meetup about the performance of JavaScript variables relative to their position in the scope chain.
My talk at the January 21, 2009 Mountain View JavaScript Meetup about the performance of JavaScript variables relative to their position in the scope chain.
Accessibility
Categories
Tags
More...Upload Details
Uploaded via SlideShare as Adobe PDF
Usage Rights
© All Rights Reserved
Statistics
- Favorites
- 16
- Downloads
- 172
- Comments
- 4
- Embed Views
- Views on SlideShare
- 8,117
- Total Views
- 8,143

In this method:
for (var i=0; i < items.length; i++) {
JS has to determine the length of items each and every loop.
Instead, you can declare an additional variable, len, and then JS needs only to do a simple comparison of the two variables each and every loop:
for (var i=0, len=items.length; ++i < len;) {
Also note the use of ++i in the comparison versus i++ as the third parameter of the for loop. This is documented as a faster incrementing method by Paul Irish: http://paulirish.com/i/d9f0.png
Happy coding,
Atg 2 years ago Reply