Objectkeys+ForEach vs ForIn

JavaScript performance comparison

Test case created by Josh

Preparation code

<script>
  var myObj = {
    "foo": "bar",
    "hello": "world",
    "key": true,
    "value": null,
    "do": "work",
    "test": 6,
    "benchmark": true
  };
</script>

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]
});
pending…
For In
for (var key in myObj) {
  if (myObj.hasOwnProperty(key)) {
    // do something with myObj[key]
  }
}
pending…

Compare results of other browsers

Revisions

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:

0 comments

Add a comment