Tipped off by Ajaxian, Gregory Reimer at Sun did some extensive testing on the fastest way to code a loop in Javascript.

The solution he found was caching the test condition and running a reverse while loop:

var i = arr.length;
while (i--) {}

The caching is obvious, because some browsers actually do take some time to find the length of HTML Object Collections. I wouldn’t have guessed that running a reverse while loop would be the fastest though.

This is definitely useful for me and something that I’ve been wanting more information on recently, after having developed a worldwide bank locator for a credit card company. If I get the chance, I might just run back and rewrite some of those loops now.

Also, if you aren’t currently subscribed to Ajaxian, check it out. It’s one of my favorite resources of the here and now.

Post Tags

Leave a Comment

Name and Email are required for posting comments. Basic HTML is supported in comments. I reserve the right to moderate comments that are off-topic, inappropriate, or spam.

Get More Paul

Powered by Habari

Visit Scribbls