Object.keys vs for in for values

JavaScript performance comparison

Revision 14 of this test case created by !

Preparation code

<script>
var keys = Object.keys;
var prop, values, index;


var obj = {
  'prop1': 'value1',
  'prop2': 'value2',
  'prop3': 'value3',
  'prop4': 'value4',
  'prop5': 'value5',
  'prop6': 'value6',
  'prop7': 'value7',
  'prop8': 'value8',
  'prop9': 'value9',
  'prop10': 'value10'
};
</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
prop = keys(obj);
index = prop.length;
values = [];
while (index-- !== 0)
  values.push(obj[prop[index]]);
pending…
for-in w/ hasOwnProperty
values = [];
for (prop in obj)
  if (obj.hasOwnProperty(prop))
    values.push(obj[prop]);
pending…
for-in w/o hasOwnProperty
values = [];
for (prop in obj)
  values.push(obj[prop]);
pending…
Object.keys using pop
prop = keys(obj);
index = prop.length;
values = [];
while (index-- !== 0)
  values.push(obj[prop.pop()]);
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