for-loop vs. for-in-loop (v12)

Revision 12 of this benchmark created on


Description

Testing the speed of iterating various sized arrays with straight for-loop or with a for-in-loop over its keys.

Preparation HTML

<script>
  // setup `arr3` array ('big')
  var arr3 = [];
  for (var m = 0; m < 5000; m++) {
   arr3.push(Math.round(Math.random()));
  }
  
  // seed `arr3_keys` hash
  var arr3_keys = {};
  for (m = 0; m < arr3.length; m++) {
   arr3.hasOwnProperty(m) && (arr3_keys[m] = 0);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
for-loop
for (var m = 0, len = arr3.length; m < len; m++) {
 arr3[m];
}
ready
for-in-loop on keys
for (var m in arr3_keys) {
 arr3[m];
}
ready

Revisions

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