Iterating over object properties

Benchmark created on


Description

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

Setup

var object = {
      foo: 0,
      bar: 1,
      baz: 2,
      method: function () {}
    }
    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
var properties = Object.keys(object);
for (var property in properties) {
  foo = object[property];
}
ready
Object.keys + forEach
Object.keys(object).forEach(function (key) {
  foo = object[key];
});
ready

Revisions

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