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" 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.

链接地址: http://www.djcxy.com/p/70004.html

上一篇: 将长度设置为变量或计算循环统计中的长度

下一篇: 在JavaScript中,字符串/数组.length属性是否执行任何处理?