ecometer

Avoid for...in loops

(Development)
#29

The for…in is more elaborate than the basic for loop because it removes duplicate list elements before enumeration. It is therefore generally more economical to use a simple for loop when the collection is well understood.

In the following loop, the for…in is poorly used:

var oSum = 0;
for( var i in oArray ) {
    oSum += oArray[i];
}

The following is better:

var oSum = 0;
var oLength = oArray.length;
for( var i = 0; i < oLength; i++ ) {
    oSum += oArray[i];
}