Object.keys().forEach() vs for in

JavaScript performance comparison

Revision 3 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var obj = {}, i = 10000, val;
   
    while ( i-- )
        obj[i] = i;
};
</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
Object.keys().forEach()
Object.keys(obj).forEach(function(obj){
    val = obj;
});
pending…
for-in
for ( var key in obj )
    if ( obj.hasOwnProperty(key) )
        val = obj[key];
pending…
Objest.keys() -> for loop
var keys = Object.keys(obj), i = 0; l = keys.length;
for (; i < l; i++  )
    val = obj[i];
pending…
Object.keys()
var keys = Object.keys(obj);
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