0

I am learning JavaScript and I see in some books/tutorial this:

myvariable = myArray.length;
for(i = 0; i < myVariable; i++){
    doSomething;
}

What's the difference if I write it like this (I mean if I don't create a variable that holds the length of my array and I use myArray.length Directly in the for loop.

for(i = 0; i < myArray.length; i++)
{
    doSomething;
}

Which one is better?

P.S: sorry for my bad English it's not my native language.

Thank you.

user2864740
  • 60,010
  • 15
  • 145
  • 220
DeltaWeb
  • 381
  • 2
  • 14
  • 2
    The second way is considered better since you don't waste a line. To your question there isn't a difference. @Chuck: No it's not. He asked about functionality not about performance. – Spencer Wieczorek May 23 '14 at 18:57
  • This question is *not* a duplicate of "Speed and memory benefit from declaring variable outside JavaScript loop?" - there is only one scope, no anonymous/HoF functions, etc. – user2864740 May 23 '14 at 19:13
  • @SpencerWieczorek: I do agree that I chose the wrong question from the list of duplicates for the reason user2864740 mentioned, but OP did not ask about functionality. The question was "Which is better?" It is definitely a duplicate question, at any rate. – Chuck May 23 '14 at 19:24
  • 2
    See http://stackoverflow.com/questions/17989270/javascript-for-loop-performance-storing-array-length-in-a-variable , http://stackoverflow.com/questions/12796887/for-loop-performance-in-javascript - some of these answer are focused on "performance", but there is also useful information which I think will cover your questions. (I use the form *without* the variable for a simple iteration over an array, due to habit/preference and following the 97/3 rule.) – user2864740 May 23 '14 at 19:26
  • Yep, that "duplicate" was completely irrelevant. Reopened. – Ry- Sep 10 '16 at 04:52

2 Answers2

2

There is no functional difference with the given code.

There is a slight performance difference (negligible in most cases) in how you declare the variables that are in the for statement.

When doing this:

for(var i = 0; i < someArray.length; i++)

var i and someArray.length are evaluated every iteration. So the total time, d, it take the loop to occur is the time, t, it takes to evaluate a variable, multiplied by the number of iterations, i, of your loop:

d = t*i

Thus, as stated early, this difference will be negligible in most cases. It takes less code to write it the first way, but doing it this way will lower d:

var i = 0,
    len = someArray.length;

for( ; i<len; i++)

However, sometimes evaluating the length of someArray every iteration is necessary due to possible changes in the length of someArray (for example, when removing elements from someArray):

for(var i = 0; i<someArray.length; i++) {
    someArray.splice(someArray.length - 1, 1);
}

This will remove the last element of someArray, and if you don't recompute the length of someArray for every iteration of the loop, some browsers will throw an error.


To see some examples of how you can write the same loop in many different ways (as well as see the performance differences), see this jsperf (a JavaScript performance test).

Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • @user2864740, What would be a better way to say it? Is it just recomputed every time? (Thus, taking into consideration the time it takes to recompute the variable?) – Josh Beam May 23 '14 at 19:13
  • @user2864740, thanks, updated my answer to reflect your comment. – Josh Beam May 23 '14 at 19:16
  • 1
    I would say that the [test expression](http://es5.github.io/#x12.6.3) is *evaluated* each loop. While evaluating `i – user2864740 May 23 '14 at 19:17
0

Approved answer is wrong. There is actually a slight performance advantage to not storing length in a variable because the storing statement operation creates an overhead.

Also note that ES6 offers for/of which has better syntax when you don't need the array index but that one is significantly slower.

See the comparison here https://jsperf.com/storing-length/1

I'm comparing 3 different versions:

 function f1(arr) {
    let l = arr.length, res=0;
    for(let i=0; i < l; i++) res+=arr[i];
    return res;
  }

  function f2(arr) {
    let res=0;
    for(let i=0; i< arr.length; i++) res+=arr[i];
    return res;
  }

  function f3(arr) {
    let res=0;
    for(const a of arr) res+=a;
    return res;
  }

When using a large sample, f1 is ~20% slower than f2, f3 is ~60% slower.

kofifus
  • 17,260
  • 17
  • 99
  • 173