caching array length (v2)

Revision 2 of this benchmark created on


Description

how fast is caching array length instead of referencing it every time

Preparation HTML

<script>
  var numberOfItems = 1000000;
  
  var myArray = [];
  
  for (var i = 0; i < numberOfItems; i++) {
   myArray[i] = Math.floor(Math.random() * i);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
without caching length
var j;
for (var i = 0; i < myArray.length; i++) {
 j = i
}
ready
with cached length
var j;
var length = myArray.length;
for (var i = 0; i < length; i++) {
 j = i
}
ready
reverse while loop
var length = myArray.length,
    j;
while (length--) {
 j = i
}
ready
reverse while loop subtract first
var length = myArray.length - 1,
    j;
while (--length) {
 j = i
}
ready

Revisions

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