开发者

In JavaScript, does the string/array .length property do any processing?

When iterating over a string or array (or anything else with a length property), I've always used a loop like this:

var foo = [...];
var i;
for(i=0; i<foo.length; i++) {
    // do something
}

However, I just encountered someone who did this:

var foo = [...];
var fooLen = foo.length;
var i;
for(i=0; i<fooLen; i++) {
    // do something
}

He said he thought the ".length开发者_StackOverflow社区" was recalculating the length, thus the loop would recalculate the length of the string/array over and over, so by saving its length to a variable it would be more optimized.

I always assumed length was just a value property because of the way it's used (it's not "asdf".length(), it's "asdf".length) but is this not the case?


There are some situations where putting the length into a local variable is faster than accessing the .length property and it varies by browser. There have been performance discussions about this here on SO and numerous jsperf tests. In a modern browser, the differences were not as much as I thought they would be, but they do exist in some cases (I can't seem to find those previous threads).

There are also different types of objects that may have different performance characteristics. For example, a javascript array may have different performance characteristics than the array-like object returned from some DOM functions like getElementsByClassName().

And, there are some situations where you may be adding items to the end of the array and don't want to be iterating through the items you add so you get the length before you start.


From MDC

for (var i = 0; i < a.length; i++) {  
    // Do something with a[i]  
}  

This is slightly inefficient as you are looking up the length property once every loop. An improvement is this:

for (var i = 0, len = a.length; i < len; i++) {  
    // Do something with a[i]  
} 


Maybe not much of a difference with "regular" arrays, but for something like "node.children.length" I would err on the safe side and call it only once. CoffeeScript does that for you automatically.

Note that there is an actual difference in behaviour if the length can change during the loop.


It depends on if you are changing the foo's length.

var foo = [1,2,3];
  while(foo.length){
  foo.shift();
}

Obviously the code is keeping track of the foo's length, not simply remembering a value.

You can assign the length as a number in the loop.

for(i=0, L=foo.length;i<L; i++) {
    // do something to foo[i]
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜