Deep copy object (jQuery vs other) (v4)

Revision 4 of this benchmark created by Leonardo Dutra 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/
  */
 /* update 4: Leonardo Dutra
  *           twitter.com/leodutra
  */
 
 deepCopyObject = function(superObj, extension) {
  if (superObj && extension) {
   var deep = function() {}; // prepare sword
   deep.prototype = superObj; // hold it
   superObj = new deep; // pull it
   return (deep = function(o, ext) { // concentrate
    var k;
    for (k in ext) {
     o[k] = typeof ext[k] === 'object' && ext[k] ? deep({}, ext[k]) : ext[k];
    }
    return o;
   })(superObj, extension); // push it deep, slicing
  }
  return null;
 };
 
 var a = { // don't forget the breath
  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); // Dead by 2 ninjas
ready

Revisions

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