object.keys vs for in (v19)

Revision 19 of this benchmark created by Patrik on


Setup

var obj = {
      a: 0,
      b: 1,
      c: 2,
      d: 3,
      e: 4,
      f: 5,
      g: 6,
      h: 7,
      i: 8,
      j: 9
    };
    var sum = 0;

Teardown


    console.log(sum);
  

Test runner

Ready to run.

Testing in
TestOps/sec
1
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
  sum += obj[keys[i]];
}
ready
2
for (var key in obj) {
  if (obj.hasOwnProperty(key)) {
    sum += obj[key];
  }
}
ready
Object.keys(obj).forEach
Object.keys(obj).forEach(function(key){
  sum += obj[key];
}
ready

Revisions

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