4

I've been unable to find the answer to this, say for example you have the array:

var myArray = ["a","b","c","d","e","f","g"]; // pretend I define up to 1000 elements

One simple approach to iterate through the array would be:

for(var i=0; i<myArray.length; i++){
    console.log(myArray[i]);
}

What I'm trying to figure out is if the .length property will be evaluated on each iteration, then if I use:

my len = myArray.length;
for(var i=0; i<len i++){
    console.log(myArray[i]);
}

Does the 2nd approach will be a performance improvement ? Does the JS engine will calculate the length on each iteration?

isJustMe
  • 5,452
  • 2
  • 31
  • 47
  • 1
    Found this exact question (or very similar): http://stackoverflow.com/questions/11821156/set-length-to-variable-or-calculate-length-within-loop-statment – tymeJV Jun 27 '13 at 14:30
  • Depends on the engine. It might be smart enough, but we can't expect it in general. Especially on older engines where it's more relevant… – Bergi Jun 27 '13 at 14:32
  • 1
    It's a property lookup, and will happen on every iteration, but it's not a "calculation" of the `.length`. It's a stored property that is updated when the Array changes in length. –  Jun 27 '13 at 14:32
  • ...and `int i=0;` isn't valid JavaScript. –  Jun 27 '13 at 14:35

2 Answers2

4

It does! I like this solution

for(var i=0, len=myArray.length; i<len; i++){
    console.log(myArray[i]);
}

It's just a little cleaner than the one you pasted.

Mulan
  • 129,518
  • 31
  • 228
  • 259
  • 3
    Never use `for in` with an array! Then `i` also becomes `length`, which is not an element of the array (but it's a property) – Oriol Jun 27 '13 at 14:34
  • Ok, I removed `for-in` example. Oriol, can you tell me why not? – Mulan Jun 27 '13 at 14:35
  • Also your first example has a syntax error, you're missing a semicolon somewhere between `i – Jamiec Jun 27 '13 at 14:35
  • 1
    See here why you shouldn't - http://stackoverflow.com/questions/500504/why-is-using-for-in-with-array-iteration-such-a-bad-idea – dsgriffin Jun 27 '13 at 14:35
  • 1
    Also, JavaScript doesn't have `int`, though I'm sure that was not noticed when copied from the question. –  Jun 27 '13 at 14:36
  • 2
    @Oriol: No, because `length` is nonenumerable. But other properties (even on the prototype) might be – Bergi Jun 27 '13 at 14:36
  • Lol, sorry I copy/pasted and modified his original code. I didn't realize all the syntax errors at first glance. – Mulan Jun 27 '13 at 14:38
  • The `.length` is non-enumerable on Arrays, but often enumerable on Array-like objects, like some host provided node collections. –  Jun 27 '13 at 14:38
  • @Bergi Ah, you're right. But some old browsers iterated over 'length', 'toString', etc. – Oriol Jun 27 '13 at 14:40
  • 1
    Another reason not to use `for...in` for arrays: The order in which the elements are iterated over is not guaranteed. And normally use arrays because order matters (I would say). – Felix Kling Jun 27 '13 at 14:41
0

Yes, but it will only be a noticeable performance improvement if this loop is executed many millions of times. However, if rather than .length, that value was a complex calculation, by all means do that, especially if it will be run millions of times.

For example, if you are looping up to length to the, say, 20th power, you should not do that every iteration.

Linuxios
  • 34,849
  • 13
  • 91
  • 116