Merge Objects vs jQuery .Externd()

JavaScript performance comparison

Revision 8 of this test case created

Info

Testing speed of js merge object properties script vs using jquery's extend method

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var settings = { validate: false, limit: 5, name: "foo" };
  var options = { validate: true, name: "bar" };

  function merge_options(a, b){
    for(var key in b)
        if(b.hasOwnProperty(key))
            a[key] = b[key];
    return a;
}

function MergeRecursive(obj1, obj2) {

  for (var p in obj2) {
    try {
      // Property in destination object set; update its value.
      if ( obj2[p].constructor==Object ) {
        obj1[p] = MergeRecursive(obj1[p], obj2[p]);

      } else {
        obj1[p] = obj2[p];

      }

    } catch(e) {
      // Property in destination object not set; create it and set its value.
      obj1[p] = obj2[p];

    }
  }

  return obj1;
};
var extmerge = function(destination) {
        var i = 1,
            ln = arguments.length,
            mergeFn = extmerge,
            object, key, value, sourceKey;
   
        for (; i < ln; i++) {
            object = arguments[i];
   
            for (key in object) {
                value = object[key];
                if (value && value.constructor === Object) {
                    sourceKey = destination[key];
                    mergeFn(sourceKey, value);
                }
                else {
                    destination[key] = value;
                }
            }
        }
   
        return destination;
    };
   
        function foreach_merge(obj1, obj2) {
                var value;
                Object.keys(obj2).forEach(function(i) {
                        value = obj2[i];
                        if (value && (value.constructor === Array || value.constructor === Object) ) {
                                if(typeof obj1[i] != 'undefined'){
                                        foreach_merge(obj1[i], obj2[i]);
                                }else{
                                        obj1[i] = obj2[i];
                                }
                        }else{
                                obj1[i] = obj2[i];
                        }
                });
                return obj1;
        }
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
JavaScript
merge_options(settings, options)
pending…
jQuery
jQuery.extend({}, settings, options);
pending…
JavaScript recursive
MergeRecursive(settings, options)
pending…
extmerge
extmerge(settings, options)
pending…
foreach_merge
foreach_merge(settings, options);
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