Saturday, 12 May 2012

Javascript For Loop troubles

Ah just got another good tip when doing for loops.

When you do this....

for(var i = 0; i < array.length; i += 1) {
? //bla bla
}

The length of the array is accessed on every loop. Slow! Slow especially when going through HTMLCollection objects.

This can get round that problem...

for(var i = 0; max = array.length; i < max; i += 1) {
? //bla bla
}

According to Stefanov this can increase the performance up to 190 times in IE7. That's a big difference!

No comments:

Comments System

Disqus Shortname