object literal iteration (v4)

Revision 4 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()
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
  res.push(obj[keys[i]]);
}
ready
Obj.key forEach bind
// 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]);
}.bind(this));
ready

Revisions

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