Array.length Performance

Claim: Using Array.length in a for loop is bad practice as the length has to be recalculated on each iteration of the loop. Running this fiddle will show how long, in milliseconds, it took to iterate through the loop and assign a variable `len` times. Replace `a.length` with `len` to see the difference.

by joshpauljohnson

HTML

<p></p>

JavaScript

var len = 10000000;
var a = new Array(len);
var start = new Date().getTime();
for (var i = 0; i < a.length; i++) {
    var b = 1;
}

$("p").text(new Date().getTime() - start);