object literal iteration (v7)

Revision 7 of this benchmark created on


Description

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

Preparation HTML

<script src="http://danielpetty.com/jquery.js"></script>

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
jQuery.each
$.each(obj, function() { res.push(this); })
ready

Revisions

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