trollacious
Programmer
A while back, xutopia wrote up some tips for optimizing Javascript code, and the question came up about what kind of loop was fastest. This can be significant in processing arrays, so the other day I tested the 3 kinds of loops in Javascript in Netscape 4.7, Netscape 6.1, and IE 5.01 to see which loop was fastest.
These are all relative numbers and the actual values irrelevant except their relationship to each other. Each loop was tested 10 times with no other processes running on the computer. The loop itself ran 1000 iterations and only called a function that went through an empty for loop 10000 times. These tests were done on the same computer within a half hour period.
IE took 930-990 ms per test for do ... while(), for, and while()... loops. There was NO difference in the time ranges between the 3 kinds of loops.
Netscape 6.1 took 1250-1310 ms per test for all 3 loops, with no difference in the tests for any of the 3 different kinds of loop.
Netscape 4.7 took 2250-2350 ms per test for all 3 loops, with no difference in the tests for any of the 3 different loops.
In some programming languages, the different kinds of looping structures take significantly different amounts of time to run, usually with the for loop being the slowest. In Javascript, with 3 different Javascript engines, each engine ran each kind of loop at the same speed, so using one type of looping structure over another isn't significant. As well, it appears that MS has its scripting engine for Javascript optimized for faster speed than Netscape, even the latest Netscape family of browser. So, if someone complains about a page building slowly that has a lot of Javascript code on it, and loops to go through, one of the first questions to ask is what browser they're using (besides how fast their computer's processor is).
I didn't test anything for a Mac or a browser other than IE and Netscape, so don't know how those would compare in speed with their own scripting engines.
These are all relative numbers and the actual values irrelevant except their relationship to each other. Each loop was tested 10 times with no other processes running on the computer. The loop itself ran 1000 iterations and only called a function that went through an empty for loop 10000 times. These tests were done on the same computer within a half hour period.
IE took 930-990 ms per test for do ... while(), for, and while()... loops. There was NO difference in the time ranges between the 3 kinds of loops.
Netscape 6.1 took 1250-1310 ms per test for all 3 loops, with no difference in the tests for any of the 3 different kinds of loop.
Netscape 4.7 took 2250-2350 ms per test for all 3 loops, with no difference in the tests for any of the 3 different loops.
In some programming languages, the different kinds of looping structures take significantly different amounts of time to run, usually with the for loop being the slowest. In Javascript, with 3 different Javascript engines, each engine ran each kind of loop at the same speed, so using one type of looping structure over another isn't significant. As well, it appears that MS has its scripting engine for Javascript optimized for faster speed than Netscape, even the latest Netscape family of browser. So, if someone complains about a page building slowly that has a lot of Javascript code on it, and loops to go through, one of the first questions to ask is what browser they're using (besides how fast their computer's processor is).
I didn't test anything for a Mac or a browser other than IE and Netscape, so don't know how those would compare in speed with their own scripting engines.