Geekpedia Programming Tutorials






How to improve the performance of your for loops

On Friday, November 2nd 2007 at 02:33 AM
By Andrew Pociu (View Profile)
***--   (Rated 3 with 2 votes)
Advertisement
More JavaScript Resources
Instead of the typical:

for(var i=0; i<theArray.length; i++)
{
     // Loop
}


Use:

for(var i=0, arrLen=theArray.length; i<arrLen; ++i )
{
     // Loop
}


Because instead of checking the length of the array each loop and comparing it to i, it stores it in a variable arrLen, which is then compared at each loop with i. This saves a few milliseconds, since checking the array length requires slightly more processing power.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this Knowledge Base article
Comment Current Comments
There are no comments.

Comment Comment on this Knowledge Base article
Name: Email:
Message:
Knowledge Base Related Knowledge Base Articles
There are no related KB articles.

Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs JavaScript Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons