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

JavaScript performance comparison

Revision 15 of this test case created

Info

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

Add quicker version of for-in without hasOwnProperty guard

Preparation code

<script src="//code.jquery.com/jquery-git2.min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    function iterate1() {
       var ret = "";
       for (var i in obj) {
          if (obj.hasOwnProperty(i)) {
            ret += obj[i];
          }
       }
       return ret;
    }
   
    function iterate2() {
       var ret = "";
       Object.keys(obj).forEach(function(key){
          ret += obj[key];
       });
       return ret;
    }
   
    function iterate3() {
       var ret = "";
       for(var key in obj) {
          ret += obj[key];
       }
       return ret;
    }
   
    var tmp;
    var obj = {
        background_color: "#cccccc",
        custom_icon_data: "{}",
        default_folder_id: 1,
        dial_columns: 4,
        dial_width: 70,
        drag_and_drop: "true",
        enable_sync: "false",
        folder_color: "#888888",
        force_http: "true",
        show_advanced: "false",
        show_folder_list: "true",
        show_new_entry: "true",
        show_options_gear: "true",
        show_subfolder_icons: "true",
        thumbnailing_service: "http://immediatenet.com/t/l3?Size=1280x1024&URL=[URL]"
    };
};

Benchmark.prototype.teardown = function() {
    tmp = "";
};
</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-in
tmp += iterate1();
pending…
Object.keys(..).forEach(..)
tmp += iterate2();
pending…
for-in (no ownproperty)
tmp += iterate3();
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