object literal iteration (v3)

Revision 3 of this benchmark created by Bovard on


Description

Testing the performances of different approaches to safe object literal iteration.

Setup

var obj = {};
    for (var i = 0; i < 10000; i++) {
      obj['key' + i] = 'value' + i;
    }
    
    var res = [];

Teardown


    res = [];
  

Test runner

Ready to run.

Testing in
TestOps/sec
forEach in Obj.keys()
// In ECMA5 Object.key(obj) returns only iterable props in the object 
// that pass obj.hasOwnProperty

Object.keys(obj).forEach(function(key) {
  res.push(obj[key]);
});
ready
prop in obj
for (var prop in obj) {
  if (obj.hasOwnProperty(prop)) {
    res.push(obj[prop]);
  }
}
ready
prop in Obj.keys()
// In ECMA5 Object.key(obj) returns only iterable props in the object 
// that pass obj.hasOwnProperty

for (var prop in Object.keys(obj)) {
  res.push(obj[prop]);
}
ready
for loop Obj.keys()
// this doesn't actually do anything

var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
  res.push(obj[keys[i]]);
}
ready

Revisions

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