Click here to Skip to main content
Click here to Skip to main content

Perfomance of for loop

By , 6 Dec 2012
 

Introduction

I like looking at the code behind of websites I admire. In doing so I think I've seen one of the most common mistakes when it comes to JavaScript performance. This is not something that would popup quickly because the effect is minimal. But I like coding, so I also like doing right.

Background

The type of loop that I often see coded wrong and which I also used to code wrong in the past is the for loop. And I believe the reason for doing it wrong is, not fully understanding what the syntax does.

This is the example from http://www.w3schools.com/js/js_loop_for.asp:

for (statement 1; statement 2; statement 3)
{
//  the code block to be executed
}

This goes on stating;

Statement 1 is executed before the loop (the code block) starts.

Statement 2 defines the condition for running the loop (the code block).

Statement 3 is executed each time after the loop (the code block) has been executed.

Although this is essentially right, they forget to say that Statement 2 will be executed every time the loop ends a code block.

The wrong example

for(var i=0; i < myarray.length; i++) {
 //do something
}

Why is this wrong?

myarray.length will be executed after every code block. Now if this array is small, the JavaScript engine will answer this question quickly. But what if the condition would be some function that could take a while?

The right example

for(var i=0, j=myarray.length; i < j; i++) {
 //do something
}

Now the length of myarray will only be executed at the start of the loop.

Conclusion

This might seem like a very little win if it comes to performance. But I believe people should always do things right. If myarray.length would be something like calculateSomeVariableLengthByRunningLengthyFunction(), this could end up as a performance penalty.

And even the w3schools tutorial doesn't explain this the right way.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sebastiaan Meijerink
Software Developer (Senior)
Netherlands Netherlands
Member
I'm a developer with 12+ years of experience. Starting of on a MVS mainframe, moving to building big multi-tier ERP systems with unix backends, to building web-based BI-Portals. I've seen a lot of different languages, environments and paradigmes.
 
At this point my main interest is webdevelopment. Mainly javascript and ASP.NET. But I also like getting my hands dirty on some PHP.
 
I've been a member of CodeProject for many years, but only recently started writing some articles. I hope someone will enjoy them.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberS. M. Ahasan Habib13 Feb '13 - 19:00 
GeneralMy vote of 5memberSamrat Banerjee9 Dec '12 - 19:30 
QuestionOnly applies circumstantially...membernich_critic7 Dec '12 - 7:20 
GeneralMy vote of 5memberJohn Isaiah Carmona6 Dec '12 - 15:50 
GeneralMy vote of 5memberSk. Tajbir6 Dec '12 - 12:05 
GeneralMy vote of 5memberHezek1ah8 Nov '12 - 9:48 
SuggestionYou should present your workingsmemberJohn Brett8 Nov '12 - 0:25 
GeneralMy vote of 5memberUsm@n7 Nov '12 - 20:34 
GeneralMy vote of 5memberdeepuam6 Nov '12 - 16:53 
QuestionReverse while is faster in most common situationsmembernschonni6 Nov '12 - 13:49 
AnswerRe: Reverse while is faster in most common situationsmemberSebastiaan Meijerink6 Nov '12 - 20:10 
GeneralRe: Reverse while is faster in most common situationsmembernschonni7 Nov '12 - 5:25 
GeneralRe: Reverse while is faster in most common situationsmemberSebastiaan Meijerink7 Nov '12 - 20:30 
GeneralGreatmemberRicardo Lynch6 Nov '12 - 7:04 
QuestionFor loop method not taughtmembersatovey6 Nov '12 - 5:57 
GeneralRe: For loop method not taughtmemberelijah.manor6 Dec '12 - 7:56 
GeneralCompiler optimization?memberpeterboulton6 Nov '12 - 5:11 
GeneralRe: Compiler optimization?memberSebastiaan Meijerink6 Nov '12 - 5:49 
GeneralRe: Compiler optimization?memberpeterboulton6 Nov '12 - 6:35 
GeneralMy vote of 5memberNeeraj_Maurya6 Nov '12 - 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 6 Dec 2012
Article Copyright 2012 by Sebastiaan Meijerink
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid