Object.keys vs for in for values

JavaScript performance comparison

Revision 7 of this test case created

Preparation code

<script>

</script>
<script>
Benchmark.prototype.setup = function() {
    var keys = Object.keys;
    var hasOwnProperty = Object.prototype.hasOwnProperty;
   
    var obj = {};
   
    for (var i = 0; ++i < 1000000;) {
      obj['key' + i] = 'value' + i;
    }
};
</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),
  index = -1,
  length = props.length,
  values = [];

while (++index < length) {
  values.push(obj[props[index]]);
}
pending…
for-in w/ hasOwnProperty
var values = [];
for (var prop in obj) {
  if (hasOwnProperty.call(obj, prop)) {
    values.push(obj[prop]);
  }
}
pending…
Object.keys for
var props = keys(obj),
  index = -1,
  length = props.length,
  values = [];

for (; ++index < length;) {
  values.push(obj[props[index]]);
}
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