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

Revision 16 of this benchmark created by Dillon Krug on


Description

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

Add Object.keys() + for loop version

Setup

function iterate1(obj) {
        var ret = "";
        for (var i in obj) {
                if (obj.hasOwnProperty(i)) {
                        ret += obj[i];
                }
        }
        return ret;
    }
    
    function iterate2(obj) {
        var ret = "";
        Object.keys(obj).forEach(function(key) {
                ret += obj[key];
        });
        return ret;
    }
    
    function iterate3(obj) {
        var ret = "";
        for (var key in obj) {
                ret += obj[key];
        }
        return ret;
    }
    
    function iterate4(obj) {
        var ret = "",
                keys = Object.keys(obj);
        for (var i = 0, l = keys.length; i < l; i++) {
                ret += obj[keys[i]];
        }
        return ret;
    }
    var tmp;
    var objLg = {
        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]",
        a_background_color: "#cccccc",
        a_custom_icon_data: "{}",
        a_default_folder_id: 1,
        a_dial_columns: 4,
        a_dial_width: 70,
        a_drag_and_drop: "true",
        a_enable_sync: "false",
        a_folder_color: "#888888",
        a_force_http: "true",
        a_show_advanced: "false",
        a_show_folder_list: "true",
        a_show_new_entry: "true",
        a_show_options_gear: "true",
        a_show_subfolder_icons: "true",
        a_thumbnailing_service: "http://immediatenet.com/t/l3?Size=1280x1024&URL=[URL]"
    };
    
    var objMd = {
        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]"
    };
    
    var objSm = {
        background_color: "#cccccc",
        custom_icon_data: "{}",
        default_folder_id: 1,
        dial_columns: 4,
        drag_and_drop: "true",
    };
    
    var objOne = {
        background_color: "#cccccc",
    };

Teardown


    tmp = "";
  

Test runner

Ready to run.

Testing in
TestOps/sec
Large — for-in
tmp += iterate1(objLg);
 
ready
Large — Object.keys(..).forEach(..)
tmp += iterate2(objLg);
 
ready
Large — for-in (no ownproperty)
tmp += iterate3(objLg);
 
ready
Large — Object.keys(..) + for loop
tmp += iterate4(objLg);
 
ready
Medium — for-in
tmp += iterate1(objMd);
ready
Medium — Object.keys(..).forEach(..)
tmp += iterate2(objMd);
ready
Medium — for-in (no ownproperty)
tmp += iterate3(objMd);
 
ready
Medium — Object.keys(..) + for loop
tmp += iterate4(objMd);
 
ready
Small — for-in
tmp += iterate1(objSm);
 
ready
Small — Object.keys(..).forEach(..)
tmp += iterate2(objSm);
 
ready
Small — for-in (no ownproperty)
tmp += iterate3(objSm);
 
ready
Small — Object.keys(..) + for loop
tmp += iterate4(objSm);
 
ready
Single — for-in
tmp += iterate1(objOne);
 
ready
Single — Object.keys(..).forEach(..)
tmp += iterate2(objOne);
 
ready
Single — for-in (no ownproperty)
tmp += iterate3(objOne);
 
ready
Single — Object.keys(..) + for loop
tmp += iterate4(objOne);
 
ready

Revisions

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