Performance of for-in loop vs for loop over Object.keys (v79)

Revision 79 of this benchmark created on


Description

After seeing http://jsperf.com/javascript-associative-vs-non-associative-arrays, I thought the test could be improved.

Setup

var arr = [],
        i,
        obj = {};
    for(i = 0; i < 10000; i += 1) {
        var o = {payload:i};
        arr.push(o);
        obj[i] = o;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
for-in loop
var sum = 0;
for (var x in obj) {
    sum += obj[x].payload;
}
ready
for loop over Object.keys
var sum = 0;
var keys = Object.keys(obj);
for (var x=0; x<keys.length; ++x) {
    sum += obj[keys[x]].payload
}
ready

Revisions

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