Iterating over object properties (v18)

Revision 18 of this benchmark created by Malleswari on


Description

Comparing various approaches to iterating over an object's enumerable properties.

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.7.0/lodash.js"></script>

Setup

var object = {};
    for (var i = 0; i < 100; ++i) {
      object['user' + i] = 'user' + i;
    }
    var foo;

Test runner

Ready to run.

Testing in
TestOps/sec
for…in
for (var property in object) {
  if (object.hasOwnProperty(property)) {
    foo = object[property];
  }
}
ready
for…in with continue
for (var property in object) {
  if (!object.hasOwnProperty(property)) continue;
  foo = object[property];
}
ready
Filtering with Object.keys
for (var i = 0, keys = Object.keys(object), l = keys.length; i < l;i++) {
  foo = object[keys[i]];
}
ready
Object.keys + forEach
Object.keys(object).forEach(function(key) {
  foo = object[key];
});
ready
_.each
_.each(object, function(value) {
  foo = value;
});
ready
Filtering with _.keys
for (var i = 0, keys = _.keys(object), l = keys.length; i < l; ++i) {
  foo = object[keys[i]];
}
ready
jQuery
$.each(object, function(key, value) {
  foo = value;
});
ready

Revisions

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