Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6679891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:24:41+00:00 2026-05-26T04:24:41+00:00

So i have known for a while that for ( var i=0, len =

  • 0

So i have known for a while that

for ( var i=0, len = myArray.length; i < len; i++ ){

}

is more efficient than

for ( var i=0; myArray.length; i++ ){

}

for large arrays. But have wondered how much of a performance gain does the former really give you ?

Today i set out to do some benchmarks. I created an array and pushed 100,000 numbers into it.

    var array = []; 
    for( var i = 0; i < 100000; i++ ) 
         array.push(i);  

I then tested both loops above by doing a console.log of each of the numbers in the array and timing the process.

console.time('loop'); 

for( var i = 0; i < array.length; i++ )
     console.log(i); 

console.timeEnd('loop');  

And for the second test

console.time('loop'); 

 for( var i = 0, len = array.length; i < len ;i++ )
     console.log(i) 

console.timeEnd('loop') 

After testing this a few times, my results are inconclusive. I get both high and low numbers for both test cases. So my question is, what’s a better test to show unequivocally that getting the length beforehand has performance benefits and what kind of percentage gain is there to doing this ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:24:42+00:00Added an answer on May 26, 2026 at 4:24 am

    Here is a relevant jsperf:
    http://jsperf.com/caching-array-length/4
    which shows that the difference can be surprisingly little.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have played around with Scala for a while now, and I know that
As you may have known that I switched from ubuntu to windows from my
I have a SOAP request that is known to work using a tool like,
I have a string that contains a known number of double values. What's the
I have a php form that has a known number of columns (ex. top
I have to develop a Java ME (formerly known as J2ME) application that will
I know that this question have been asked several times. But I can't get
I have a view that will create a table while enumerating over each item
I have long known that you can load style rules into a page dynamically
I have some AS3 MP3 code that my teacher gave me a while ago

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.