Loop Through Array Without Wasteful Lookups

Find the length of the array before using it in the for function, so it doesn't need to count the length of the array each iteration (assuming the length won't be changing mid-loop).

var arLen=myArray.length;

for ( var i=0, len=arLen; i<len; i++ ){
  // do something with myArray[i]


  1. User Avatar
    Permalink to comment#

    While you are correct that it speeds things up, it’s not because the alternative has to count the length each time. With a language like java where the length/size/count method on the array object is in fact a method (that presumably does some calculation to figure out the size) then it is better to cache the size in the way you described.

    The reason that it is slightly faster this way, is because the browser does have to dereference the array object each time it gets its length, not recalculate the length.

    Also if you want to make another very slight improvement here you can do:

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

    where the key here is the ++i instead of i++.

    • User Avatar
      Permalink to comment#

      Why is this faster?

    • User Avatar
      Permalink to comment#

      The post-increment operator (i++) first returns the current value of i and then increments it by one. The pre-increment operator (++i) first increments the value and then returns the value.

      As you can see, the compiler needs to hold the previous state of i as return value for i++ whereas for ++i it returns the incremented state. The difference is really minuscule though but hey, we just saved an otherwise wasted lookup. ;-)

  2. User Avatar
    Permalink to comment#

    Another tiny trick that may help speed things up is to count down to zero if possible, as it is quicker to do a compare to zero, than to do a ‘less than’ (which is fundamentally a subtraction followed by a comparison with zero.

  3. User Avatar
    Sumeet Chawla

    One more thing I would like to suggest is that why have you used another variable len to store the value of the length of the array:


    why not just:

    var arLen=myArray.length;

    for ( var i=0; i<arLen; i++ ){
    // do something with myArray[i]

    Very interesting points for a simple loop structure :) Really very conceptual.. Love the comments!

    • User Avatar
      sireesh reddy

      hi u r absolutely correct .i want to loop through an array values and apply css will u please tell me.

  4. User Avatar
    Permalink to comment#

    Why not ditch both variables and simplify the test condition even further?

    instead of:



    var arLen=myArray.length;

    how about this:

    var i = myArray.length;
    while (i–) {
    // do something with myArray[i]

  5. User Avatar
    Permalink to comment#

    this is benchmark for this topic http://jsperf.com/for-loop-research

  6. User Avatar
    Permalink to comment#

    I think we can further optimize it. As per my understanding arLen should be iLen. arLen refer as if this is a variable of array type but this is an integer variable. We always follow best variable naming conventions concerning future code maintenance.

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.