Click here to Skip to main content
15,118,967 members
Articles / Programming Languages / JScript .NET
Posted 6 Nov 2012


13 bookmarked

Perfomance of for loop

Rate me:
Please Sign up or sign in to vote.
4.85/5 (13 votes)
6 Dec 2012CPOL1 min read
Lots of for loops are coded wrong causing a potential performance issue.


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.


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

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.


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.


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
I'm a developer with 15+ 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.

My main focus has been shifting towards full javascript the past years. Almost anything can be accomplished now. From building full offline webapps, to IoT (Tessel), to server (node).

Comments and Discussions

GeneralMy vote of 5 Pin
S. M. Ahasan Habib13-Feb-13 20:00
professionalS. M. Ahasan Habib13-Feb-13 20:00 
GeneralMy vote of 5 Pin
Samrat Banerjee9-Dec-12 20:30
MemberSamrat Banerjee9-Dec-12 20:30 
QuestionOnly applies circumstantially... Pin
nich_critic7-Dec-12 8:20
Membernich_critic7-Dec-12 8:20 
GeneralMy vote of 5 Pin
John Isaiah Carmona6-Dec-12 16:50
professionalJohn Isaiah Carmona6-Dec-12 16:50 
GeneralMy vote of 5 Pin
Sk. Tajbir6-Dec-12 13:05
MemberSk. Tajbir6-Dec-12 13:05 
GeneralMy vote of 5 Pin
Hezek1ah8-Nov-12 10:48
MemberHezek1ah8-Nov-12 10:48 
SuggestionYou should present your workings Pin
John Brett8-Nov-12 1:25
MemberJohn Brett8-Nov-12 1:25 
GeneralMy vote of 5 Pin
Usm@n7-Nov-12 21:34
MemberUsm@n7-Nov-12 21:34 
GeneralMy vote of 5 Pin
deepuam6-Nov-12 17:53
Memberdeepuam6-Nov-12 17:53 
QuestionReverse while is faster in most common situations Pin
nschonni6-Nov-12 14:49
Membernschonni6-Nov-12 14:49 
AnswerRe: Reverse while is faster in most common situations Pin
Sebastiaan Meijerink6-Nov-12 21:10
professionalSebastiaan Meijerink6-Nov-12 21:10 
GeneralRe: Reverse while is faster in most common situations Pin
nschonni7-Nov-12 6:25
Membernschonni7-Nov-12 6:25 
GeneralRe: Reverse while is faster in most common situations Pin
Sebastiaan Meijerink7-Nov-12 21:30
professionalSebastiaan Meijerink7-Nov-12 21:30 
GeneralGreat Pin
Ricardo Lynch6-Nov-12 8:04
MemberRicardo Lynch6-Nov-12 8:04 
QuestionFor loop method not taught Pin
satovey6-Nov-12 6:57
Membersatovey6-Nov-12 6:57 
GeneralRe: For loop method not taught Pin
elijah.manor6-Dec-12 8:56
Memberelijah.manor6-Dec-12 8:56 
GeneralCompiler optimization? Pin
peterboulton6-Nov-12 6:11
professionalpeterboulton6-Nov-12 6:11 
GeneralRe: Compiler optimization? Pin
Sebastiaan Meijerink6-Nov-12 6:49
professionalSebastiaan Meijerink6-Nov-12 6:49 
GeneralRe: Compiler optimization? Pin
peterboulton6-Nov-12 7:35
professionalpeterboulton6-Nov-12 7:35 
GeneralMy vote of 5 Pin
Neeraj_Maurya6-Nov-12 4:10
MemberNeeraj_Maurya6-Nov-12 4:10 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.