Caching array's length (v4)

Revision 4 of this benchmark created by Lord of Rape on


Description

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

Setup

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

Test runner

Ready to run.

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

Revisions

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