Caching array's length

Benchmark created by david Catuhe on


Description

Should I cache the length of the array or can I access it every time in the loop?

Setup

var myArray = [];
    
    var length = 50000;
    
    for (var i = 0; i < length; i++) {
      myArray.push(Math.random());
    }

Test runner

Ready to run.

Testing in
TestOps/sec
With length
var total = 0;
for (var i = 0; i < myArray.length; i++) {
  total += myArray[i];
}
ready
cached length
var total = 0;
for (var i = 0; i < length; i++) {
  total += myArray[i];
}
ready

Revisions

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