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

Revision 10 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'
    };
    
    var keys = Object.keys(obj);

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 i = keys.length;

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

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

Revisions

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