Object.keys vs for in for values

JavaScript performance comparison

Revision 28 of this test case created

Preparation code

<script>

</script>
<script>
Benchmark.prototype.setup = function() {
    var keys = Object.keys;
    var hasOwnProperty = Object.prototype.hasOwnProperty;
    var gl = {
      game: 'Football',
      team: 'ABC',
      qb: 'Manning',
      wr: 'calvin',
      te: 'davis'
    };
   
    var params = {
      qb: 'Manning'
    };
   
    var obj = {
      name: function() {
        return params.name || gl.name
      },
      qb: function() {
        return params.qb || gl.qb
      },
      wr: function() {
        return params.wr || gl.wr
      },
      te: function() {
        return params.te || gl.te
      }
    };
};
</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 props = keys(obj),
  len = props.length,
  index = -1,
  values = {};

while (++index < length) {
  values[props[index] = obj[props[index]]();
  }
pending…
for-in w/ hasOwnProperty
var values = {};
for (var prop in obj) {
  if (obj.hasOwnProperty(prop)) {
    values[prop] = 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