for-in versus Object.keys(..).forEach(..) (v34)

Revision 34 of this benchmark created on


Description

Testing if the perf hit for Object.keys(..).forEach(..) is enough to matter as compared to for-in with a hasOwnProperty(..) check.

Preparation HTML

<script>
function iterate1() {
   var count = 0;
   for (i in obj) {
      count++;
   }
   return count;
}

function iterate2() {
   var count = 0, keys = Object.keys(obj);
   for (var i = 0; i < keys.length; i++) {
       count++;
   }
}

// object with 10 keys in it
var obj = {
   a: "a",
   b: "b",
   c: "c",
   d: "d",
   e: "e",
   f: "f",
   g: "g",
   h: "h",
   i: "i",
   j: "j"
};

var tmp;
</script>

Setup

tmp = "";

Test runner

Ready to run.

Testing in
TestOps/sec
for-in
tmp += iterate1();
ready
Object.keys(..).forEach(..)
tmp += iterate2();
ready

Revisions

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