object iteration

JavaScript performance comparison

Revision 2 of this test case created by Arnor

Info

Which case is faster for iterating of an object with much keys.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = {};
     
    for(var i=0; i < 65536; i++) {
        obj[i] = "a";
    }
   
    var empty = [];
};
</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
for-with-hasOwnProperty
for(var x in obj) { 
        if(obj.hasOwnProperty(x)) {
        empty.push(x);
        }
};
pending…
object-keys-forEach
Object.keys(obj).forEach(function(v) {
                empty.push(v);

});
pending…
object-keys-for
var arr = Object.keys(obj);
for(var x = 0, l = arr.length; x < l; x++) {
        empty.push(x);
}
pending…
object-keys-for 2
var arr = Object.keys(obj);
for(var x = 0; x < arr.length; x++) {
        empty.push(x);
}
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