Object.keys vs for..in (v21)

Revision 21 of this benchmark created by Nathan on


Preparation HTML

<script>
var obj = {
  a: 0,
  b: 1,
  c: 2,
  d: 3,
  e: 4,
  f: 5,
  g: 6,
  h: 7,
  i: 8,
  j: 9
};
var sum = 0;
</script>

Setup

sum = 0;

Test runner

Ready to run.

Testing in
TestOps/sec
Object.keys
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
  sum += obj[keys[i]];
}
ready
for..in with hop
for (var key in obj) {
  if (obj.hasOwnProperty(key)) {
    sum += obj[key];
  }
}
ready
plain for..in
for (var key in obj) {
  sum += obj[key];
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.