Cutting down on loop iterations with labels
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”:http://muffinresearch.co.uk 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”:http://icant.co.uk/sandbox/loopLabelDemo.html .
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:
- “How to create”:http://www.howtocreate.co.uk/tutorials/javascript/controls
- “HVCC”:http://academ.hvcc.edu/~kantopet/old/javascript/index.php?page=js+exiting+loops&parent=js+statements
[tags]webdevtrick[/tags]


May 1st, 2006 at 12:46 pm
May 1st, 2006 at 12:47 pm
May 25th, 2006 at 9:06 am
September 14th, 2006 at 8:45 am
April 28th, 2006 at 5:47 pm
Now how is it that I’ve never seen this before? You just saved my poor computer about a million loop iterations per day with my Greasemonkey scripts alone.
May 2nd, 2006 at 9:40 am
Oh, excellent! Great tip
May 2nd, 2006 at 5:58 pm
Great article – however you never answered the first question about how supported they are.
Also, it would be great if you would write about “break”s friend, “continue” – another way to help make your loops more friendly.
May 8th, 2006 at 8:08 am
I tested it to work on IE4+, so I suppose it should work on anything that supports JS.
May 11th, 2006 at 2:43 am
How is that different from putting the whole thing in a function and doing using a return?
May 12th, 2006 at 1:14 pm
Nice post, I’ve just shown it to the other developers at work to combined sounds of ‘Ohhhh’ (read the ‘Ohhh’ in a French accent to really set the scene).
Nice post – concise but nice.
Regards,
Brian