Deep copy object (jQuery vs other) (v3)

Revision 3 of this benchmark created on


Description

Deep copy/extend objects, jQuery versus other approach(es)

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
 
<script>
 /*!
  * Copyright Andrée Hansson, 2010
  * Use it however you want, attribution would be nice though.
  *
  * GMail/Twitter:  peolanha
  * IRC (FreeNode): peol
  * Website:        http://andreehansson.se/
  */
 
 !
 function() {
  window.deepCopyObject = function(extendee, extender) {
   for (var o in extender) {
    extendee[o] = getValue(extender[o]);
   }
 
   return extendee;
  }
 
  function getValue(obj) {
   var
   isArray = typeof obj === 'object' && obj.slice,
       isObject = typeof obj === 'object',
       val, i = 0,
       l;
 
   // Array
   if (isArray) {
    val = Array.prototype.slice.apply(obj);
    l = val.length;
 
    do {
     val[i] = getValue(val[i]);
    }
    while (++i < l);
   }
 
   // Object
   else if (isObject) {
    val = deepCopyObject({}, obj);
   }
 
   // Others
   else {
    val = obj;
   }
 
   return val;
  }
 }();
 
 var a = {
  a: '1',
  b: {
   a: '1',
   b: [{
    a: '1'
   },
   {
    b: '1'
   },
   {
    c: '1'
   },
   {
    d: '1'
   }]
  }
 };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery
$.extend(true, {
 c: '1'
}, a);
ready
Other
deepCopyObject({
 c: '1'
}, a);
ready

Revisions

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