Christian Heilmann

## Posts Tagged ‘optimization’

### The only for loop you will ever need °

Monday, April 2nd, 2007

° main contain hyperbole to take the mickey out of other blog posts

I am a very lazy person when it comes to typing. As I am doing so much of it I’d rather not type something I really don’t need. This is why I had an issue with “for” loops as you need to type them so often.

The classic way of looping through an array for example is a for loop with an iterator:

```var arr = [1,2,3,4,5]; for( var i=0; i<arr.length; i++ ){ alert( arr[i] ); }```

This is however not a good plan for large arrays (like DOM trees) as the length attribute is read out every time the loop executes. You can avoid this in two ways. If it is for example irrelevant in which order you loop through the array you can use a reverse loop:

```var arr = [1,2,3,4,5]; for( var i=arr.length - 1; i>-1; i-- ) { alert( arr[i] ); }```

Or using while:

```var arr = [1,2,3,4,5]; var i = arr.length; while( i-- ) { alert( arr[i] ); }```

If you really need to go through the array in the normal order then you need to use another variable to store the length and compare with that one.

```var arr = [1,2,3,4,5]; for( var i=0,j=arr.length; i<j ; i++ ){ alert( arr[i] ); }```

The for loop also allows you to use the in keyword to make it easier:

```for(i in arr ){ alert(arr[i]); }```

However, there is also another trick if you don’t want to do this (for example if you don’t want to iterate through all but use a step of 2). The comparison inside the loop does not need to be the iterator but could be any comparison. As long as it is true, the loop gets executed, when it becomes false the loop ends. This allows you to simply check for the array element inside the loop itself which means you don’t need to read the length attribute at all:

```var arr = [1,2,3,4,5]; for(var i=0; arr[i]; i++ ){ alert( arr[i] ); }```

There is however the issue of the truthy and falsy and type casting in JavaScript which means that if your array has null or 0 elements the loop would end, too:

```var arr = [1,2,0,4,5]; for(var i=0; arr[i]; i++ ){ alert( arr[i] ); }```

You can avoid this by testing properly:

```var arr = [1,2,0,4,5]; for(var i=0; arr[i]!==undefined; i++ ){ alert( arr[i] ); }```

There is also another problem this kind of loop avoids: if you manipulate the array during execution of the loop (for example by removing or moving DOM nodes) you need to change the iterator, too. This example will be an endless loop and lock your browser:

```var u = document.getElementsByTagName('ul')[0]; var lis = u.getElementsByTagName('li'); for( var i=0; i&lt;lis.length; i++ ){ u.removeChild(lis[i]); }```

With the other kind of loop that is not a problem. You can for example remove all list items from a UL with:

```var u = document.getElementsByTagName('ul')[0]; var lis = u.getElementsByTagName('li'); for( var i=0; lis[0]; i++ ){ u.removeChild( lis[0] ); }```

Did I miss something obviously bad about this idea? If so, please tell me, but also consider the other benefits of this for loop to end all for loops:

• a lot smaller than the YUI, Dojo or Prototype
• unobtrusive and accessible (if the array is not there nothing happens)
• may have rounded corners (but they are invisible)
• cross-browser