1

In my Javascript reference book, for loops are optimized in the following way:

for( var i = 0, len = keys.length; i < len; i + +) { BODY }

Apparently, doing "len = keys.length" prevents the computer from recalculating keys.length each time it goes through the for loop.

I don't understand why the book doesn't write "var len = keys.length" instead of "len = keys.length"? Isn't the book making "len" a global variable, which isn't good if you're trying to nest two for-loops that loop through two arrays?

E.g.

for( var i = 0, len = keys.length; i < len; i + +) { 
     for (var i = 0; len = array2.length; i < len; i++) {
     }
}

Source: Flanagan, David (2011-04-18). JavaScript: The Definitive Guide: Activate Your Web Pages (Definitive Guides) (Kindle Locations 6992-6998). O'Reilly Media. Kindle Edition.

1
  • 6
    var i = 0, len = keys.length is shorthand for var i=0; and var len=key.length Commented May 23, 2015 at 5:30

3 Answers 3

2

You can chain variable declarations like this

var i = 0, foo="bar", hello = "world"

Which is close to the same thing as

var i = 0;
var foo = "bar";
var hello = "world";

So this

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

Is the same as

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

I like to avoid .length altogether and use something like

var keys = /* array or nodelist */, key, i;
for(i = 0; key = keys[i]; i++)

This will cause the for loop to end as soon as key resolves to undefined. Now you can use key instead of keys[i]

As a side note, your second example would never work as all of the variables defined in the first for statement would be overwritten by the second, yielding unexpected results. You can nest for loops, but you have to use different variable names.

Sign up to request clarification or add additional context in comments.

1 Comment

I used to use and promote a loop style very similar to your last example. However, I don't use it any more. Surprisingly, it is much slower than a conventional loop in modern browsers, because reading past the end of the array causes the entire array to become de-optimized. Here are some test results for various kinds of loops.
1

AS Ankit correctly mentioned in the comments of the question, it is a shorthand. And moreover, if I am correct, javascript is functionally scoped and not block scoped, so you are overwriting the len declared in the outer for loop while re-declaring it in the inner one.

Comments

0

In Javascript each time you instantiate it's a heavy cost. Using one var and short hand commas to create multiple variables is much more efficient.

Your example using only one var to create a variable.

 for (var i = 0, len = array2.length; i < len; i++) {
      //loop
 }

The alternative is to var up the len variable outside the loop.

 var len = array2.length; 
 for (var i = 0; i < len; i++) {
      //loop
 }

Now you have two separate "var" instances.

I personally like to declare my var at the beginning.

 var len = array2.length,
     i = 0;

 for(i = 0; i < len; i++){
      //First loop
 }

 //So if I use more than one for loop in a routine I can just reuse i
 for(i = 0; i < 10; i++){
      //Second loop
 }

Hope that helps explain.

1 Comment

Whether you use one var or two won't affect performance in the slightest. It's really a matter of which style you prefer. (Also note a typo in your first example.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.