Different kinds of loop

Benchmark created on


Preparation HTML

<script>
  var arr = [], obj = {}, count = 0;
  for( var i = 0; i < 10000; i++ ) {
      arr[i] = 'value' + i;
      obj[i] = 'value' + i;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
arr.forEach
arr.forEach(function(i) {
    count++;
});
ready
var i in arr
for( var i in arr ) {
    count++;
}
ready
var i in arr (with hasOwnProperty check)
for( var i in arr ) {
    if( !arr.hasOwnProperty( i ) ) continue;
    count++;
}
ready
var i in obj
for( var i in obj ) {
    count++;
}
ready
var i in obj (with hasOwnProperty check)
for( var i in obj ) {
    if( !obj.hasOwnProperty( i ) ) continue;
    count++;
}
ready
for i < arr.length (without caching)
for( var i = 0; i < arr.length; i++ ) {
    count++;
}
ready
for i < arr.length (with caching)
for( var i = 0, max = arr.length; i < max; i++ ) {
    count++;
}
ready

Revisions

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