Christian Heilmann

Cutting down on loop iterations with labels

Friday, April 28th, 2006 at 1:48 pm

Just a quick reminder that you can drastically cut down on loop iterations by using the break and continue commands, and that there is an option to label loops to allow nested loops to stop their parents from iterating.

The Muffin Man Stuart Colville asked me earlier today how supported labels are in loops and breaks and I was confused at first as I had not heard or – more likely – forgotten about that feature.

Normally you have a loop and you go through it:

for(i=0; i<200; i++){
  if ( i==10 ) {
    alert ('10');
  }
}

This would execute 200 times. If you use a break, it only executes 10 times:


for(i=0; i<200; i++){
if ( i==10 ) {
alert (‘10’);
break;
}

}

If you have to nest loops though, the break would only stop the inner loop. The following example would still execute 2200 times:


for(i=0; i<200; i++){
for(j=0; j<200; j++){
if ( j==10 ) {
alert (‘10’);
break;
}

}
}

Now, the loop can get a label, which is a word followed by a colon, and that can be used to break or continue different loops. The following example only executes 11 times.


outer:for(i=0; i<200; i++){
for(j=0; j<200; j++){
if ( j==10 ) {
alert (‘10’);
break outer;
}

}
}

Using labels can help immensely on cutting down on loop iterations – and make scripts faster. Say for example you have a JSON object that is a search result:


results={
‘CSS’:{
books:[
{

‘title’:’Bulletproof CSS’,
‘author’:’Dan Cederholm’
},
{

‘title’:’Professional CSS’,
‘author’:’Various’
},
{

‘title’:’Eric Meyer On CSS’,
‘author’:’Eric Meyer’
}

],
web:[
{

‘title’:’CSS discuss’,
‘maintainer’:’Eric Meyer’
},
{

‘title’:’Simplebits’,
‘author’:’Dan Cederholm’
},
{

‘title’:’456 Berea Street’,
‘author’:’Roger Johannson’
}

]
},
‘JavaScript’:{
books:[
{

‘title’:’DOM Scripting’,
‘author’:’Jeremy Keith’
},
{

‘title’:’Beginning JavaScript with DOM scripting and AJAX’,
‘author’:’Christian Heilmann’
},
{

‘title’:’JavaScript:The Definitive Guide’,
‘author’:’David Flanagan’
}

]
}

}

If you send the author and the media as query data, for example “Roger Johannson” and “web” you can use a normal nested loop:


for(i in results){
for(var j in results[i]){
for(var k in results[i][j]){
if(results[i][j][k][‘author’]==author){
alert(results[i][j][k][‘title’]);
}

}
}

}

This would be executed 9 times. If you use a break, you can cut it down to 8 times.


for(i in results){
for(j in results[i]){
for(k in results[i][j]){
if(results[i][j][k][‘author’]==author){
alert(results[i][j][k][‘title’]);
break;
}

}
}

}

If you use a label on the main loop – for example “lookup” – and break the parent loop instead of the innermost one, it is 5 iterations.


lookup:for(i in results){
for(j in results[i]){
for(k in results[i][j]){
if(results[i][j][k][‘author’]==author){
alert(results[i][j][k][‘title’]);
break lookup;
}

}
}

}

If you also test for the media and continue that loop the whole construct will execute 2 times.


lookup:for(i in results){
media:for(j in results[i]){
for(k in results[i][j]){
if(j!=media) continue media;
if(results[i][j][k][‘author’]==author){
alert(results[i][j][k][‘title’]);
break lookup;
}

}
}

}

You can see all the functions and results on the demo page .

Of course, another option would be to not break the loop, but put the lot in an own function/method and return to the main script with the result. However, this technique can collect several results before commencing with the rest of the functionality.

Again, thanks to Stuart for bringing that up. Other resources:

Share on Twitter