June 4, 2008
If we were iterating over the elements in an array or map a standard looking variant loop would look like
for(var i = 0; i < array.length; i++)
An invariant loop would be
var len = array.length;
for(var i = 0; i < len; i++)
and a for each loop would look like
for (var i in array)
While the original tests were looking for percent speed improvements within a browser achievable by selecting the different types of loops I thought it would be interesting to modify the test to report raw speeds in milliseconds and then compare various browsers.
The speeds reported here are for the fast invariant for loop over a series of runs.
Safari 3.0 – 98 milliseconds
WebKit 3.1.1 – 37 milliseconds
Firefox 188.8.131.52 – 200 milliseconds
Firefox 3.0 RC1 – 28 milliseconds
It will be interesting to see if this new version makes it into the upcoming iPhone OS 2.0. It would be fantastic if it was stable enough and did. Every bit of speed you can get on a handheld device is precious.