caching array length

Benchmark created by Anurag 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
for (var i = 0; i < myArray.length; i++) {}
ready
with cached length
var length = myArray.length;
for (var i = 0; i < length; i++) {}
ready
reverse while loop
var length = myArray.length;

while (length--) {}
ready

Revisions

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