For..in loop with and without hasOwnProperty (v6)

Revision 6 of this benchmark created on


Description

Showing the performance difference between a for..in loop using hasOwnProperty and without.

Setup

var obj = {
        somekey: 'somevalue',
        somekey2: 'somevalue',
        somekey3: 'somevalue'
    };

Test runner

Ready to run.

Testing in
TestOps/sec
For..in without hasOwnProperty check
for (var val in obj) {
    console.log(val);
}
ready
For..in with hasOwnProperty check
for (var val in obj) {
    if (obj.hasOwnProperty(val)) {
        console.log(val);
    }
}
ready
Reverse While Loop
var keys = Object.keys(obj);
var i = keys.length;

while(i--){
    console.log(keys[i]);
}
ready
While Loop Incremented
var keys = Object.keys(obj);
var l = keys.length;
var i = 0;

while (i < l) {
  console.log(keys[i]);
  i += 1;
}
ready
For loop
var keys = Object.keys(obj);
var l = keys.length;
var i = 0;

for(;i<l;i++) {
   console.log(keys[i]);
}
ready

Revisions

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