length usage

Benchmark created on


Description

Apparently : Cache Length During Loops In a for loop, don't access the length property of an array every time; cache it beforehand.

Test runner

Ready to run.

Testing in
TestOps/sec
Improper
var myArray = [1, 2, 3, 4];
 
for ( var i = 0; i < myArray.length; i++ ) {
    console.log(i);
    // do stuff
 
}
ready
Proper
var myArray = [1, 2, 3, 4];
var myLength = myArray.length;
 
for ( var i = 0; i < myLength; i++ ) {
    console.log(i);
    // do stuff
 
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.