For in vs loop with object.keys() (v3)

Revision 3 of this benchmark created on


Description

Since for in loops are so slow, can we make it any faster with using Object.keys(obj).

Setup

var temp = {
      x: 0,
      y: 0,
      z: 0
    };

Test runner

Ready to run.

Testing in
TestOps/sec
Standard for in loop
for (var i in temp) {
  console.log(temp[i]);
}
ready
for loop with obj key
var obj = Object.keys(temp);
var length = obj.length;

for (var i = 0; i != length; i++) {
  console.log(temp[obj[i]]);
}
ready
while loop with object key (reverse order)
var obj = Object.keys(temp);
var i = obj.length - 1;

while (i) {
  console.log(temp[obj[i]]);
  i--;
}
ready
while loop with object key (correct order)
var obj = Object.keys(temp);
var length = obj.length;
var i = length;

while (i) {
  console.log(temp[obj[length - i]]);
  i--;
}
ready

Revisions

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