Objectkeys+ForEach vs ForIn

JavaScript performance comparison

Test case created by Josh

Preparation code

  var myObj = {
    "foo": "bar",
    "hello": "world",
    "key": true,
    "value": null,
    "do": "work",
    "test": 6,
    "benchmark": true

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Object Keys + forEach
Object.keys(myObj).forEach(function (key) {
  // do something with myObj[key]
For In
for (var key in myObj) {
  if (myObj.hasOwnProperty(key)) {
    // do something with myObj[key]

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:


Comment form temporarily disabled.

Add a comment