Do you really know how for-loops work? How to speed up your for loops

Did you know that the second parameter of a three parameter for loop gets executed on every loop? When this parameter is an expensive operation this could slow down your application. Of course I knew the second parameter gets executed every time, but I didn’t realize what kind of impact this had until I read an article and ran some tests. This ‘theory’ applies to every programming language with for-loops so it may be useful to every programmer/scripter.
When I ran some tests on Javsascript I discovered some for loops could be a 100 times faster!
....

Probably every programmer knows the second parameter is executed on every loop. But I think a lot of developers don’t realize the impact of this. I recently read an article about improving Javascript that opened my eyes. The tip in the article had something to do with assigning function pointers to local variables when you use them a lot, with a for-loop as an example.
Let me demonstrate what happens with a simple java example. I know everybody knows the outcoume, but you can’t see it enough.

public void testLoop() {
for (int i = 0; i < getLength(); i++) {
// do nothing
}
}

private int getLength() {
System.out.println("Are we there yet?");
return 10;
}

“Are we there yet?” is printed 10 times. In this case (and most cases for for-loops) we only need to execute the getLength() method 1 time because we know it’s value stays the same. When the getLength() method was a very expensive operation it was executed 9 times too much (with a cheap operation too, but you wouldn’t have noticed)

I ran some test on Javascript and that really scared me. I created a table with 72 cells and looped through every cell with:

document.getElementById('t').getElementsByTagName('td')

Stupid as I am my for loop looks like this:

for (var i = 0; i < document.getElementById('t').getElementsByTagName('td').length; i++) {
}

And it should like this:

var l = document.getElementById('t').getElementsByTagName('td').length;
for (var i = 0; i < l; i++) {
}

So let’s cut to the case. These two snippets produce exactly the same result, the second example though runs about a 160 times faster in my FireFox and about 100 times faster in Internet Explorer. Wow! And this doesn’t look like an expensive operation in my opinion. Note that I ran the test several times and the for-loops were nested inside another for loop (the first set ran 5000 times and the second set 50000 to get a good idea of how long it took)

In java I created an ArrayList with about 40 Strings in it and did the same for-loop test. The list.size(); test was about 6 times slower than assinging a local variable.

Don’t think you can speed up every for loop 6 times (or 100 times), usually for loops are not empty, but assigning a local variable might improve things a bit, especially for a huge load of loops. The slow empty for-loop in Javascript took 1.6ms each time, with 72 cells this is already noticable.

Conclusion

So what should you do know? Search for for-loops throughout your application and search for expensive operations in your for loop. When you’re a Javascripter check al DOM-operations in for-loops, these operations are relatively slow and you usually need the operation only once. It’s also a good idea to read the Javscript article, it might help you. Look for .length in Javascript, .length seems innocent, but it might just give you the performance boost you need.
It’s code-wise also better to assign the second parameter to a local variable, it makes your code more readable and why make unnecessary method calls?
I know this article wan’t rocket science, but a lot of people forget the impact of a method call in a for-loop.

Source

IEBlog : IE + JavaScript Performance Recommendations – Part 1

10 Comments

  1. Infernoz August 2, 2007
  2. HuSoft July 18, 2007
  3. Nishant Saini July 18, 2007
  4. Fikovnik July 18, 2007
  5. Fikovnik July 18, 2007
  6. p3t0r July 17, 2007
  7. Matt Giuca July 16, 2007
  8. Jeroen van Wilgenburg July 16, 2007
  9. prashant July 16, 2007
  10. sezgin July 16, 2007