Object.keys vs for in w/ and w/out hasOwnProperty for values (10 values)

JavaScript performance comparison

Revision 60 of this test case created by Brandon Papworth

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = {
      foo1: 1401006136982,
      foo2: 1401006136982,
      foo3: 1401006136982,
      foo4: 1401006136982,
      foo5: 1401006136982,
      foo6: 1401006136982,
      foo7: 1401006136982,
      foo8: 1401006136982,
      foo9: 1401006136982,
      foo10: 1401006136982
    };
    var _hasOwnProperty = Object.prototype.hasOwnProperty;
};
</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
var keys = Object.keys(obj),
  values = [];
for (var i = 0, m = keys.length; i < m; i++) {
  values.push(obj[keys[i]]);
}
pending…
for-in w/out hasOwnProperty
var values = [];
for (var prop in obj) {
  values.push(obj[prop]);
}
 
pending…
for-in w/ hasOwnProperty
var values = [],
  prop;
for (prop in obj) {
  if (obj.hasOwnProperty(prop)) {
    values.push(obj[prop]);
  }
}
pending…
for-in w/ hasOwnProperty called & scoped
var values = [],
  prop;
for (prop in obj) {
  if (_hasOwnProperty.call(obj,prop)) {
    values.push(obj[prop]);
  }
}
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