cloning an object

JavaScript performance comparison

Revision 53 of this test case created by nagaozen

Info

There is no quick and easy facility for cloning an object, Some people recommend using JQuery.extend others JSON.parse/stringify

http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-a-javascript-object

If you want the fastest possible clone function. I would personally anticipate the data structure of your object and write a custom clone to handle it.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/immutable.js/3.6.4/immutable.min.js"></script>
<script>

var clone = (function() {
'use strict';

/**
 * Clones (copies) an Object using deep copying.
 *
 * This function supports circular references by default, but if you are certain
 * there are no circular references in your object, you can save some CPU time
 * by calling clone(obj, false).
 *
 * Caution: if `circular` is false and `parent` contains circular references,
 * your program may enter an infinite loop and crash.
 *
 * @param `parent` - the object to be cloned
 * @param `circular` - set to true if the object to be cloned may contain
 *    circular references. (optional - true by default)
 * @param `depth` - set to a number if the object is only to be cloned to
 *    a particular depth. (optional - defaults to Infinity)
 * @param `prototype` - sets the prototype to be used when cloning an object.
 *    (optional - defaults to parent prototype).
*/

function clone(parent, circular, depth, prototype) {
  var filter;
  if (typeof circular === 'object') {
    depth = circular.depth;
    prototype = circular.prototype;
    filter = circular.filter;
    circular = circular.circular
  }
  // maintain two arrays for circular references, where corresponding parents
  // and children have the same index
  var allParents = [];
  var allChildren = [];

  var useBuffer = typeof Buffer != 'undefined';

  if (typeof circular == 'undefined')
    circular = true;

  if (typeof depth == 'undefined')
    depth = Infinity;

  // recurse this function so we don't reset allParents and allChildren
  function _clone(parent, depth) {
    // cloning null always returns null
    if (parent === null)
      return null;

    if (depth == 0)
      return parent;

    var child;
    var proto;
    if (typeof parent != 'object') {
      return parent;
    }

    if (clone.__isArray(parent)) {
      child = [];
    } else if (clone.__isRegExp(parent)) {
      child = new RegExp(parent.source, __getRegExpFlags(parent));
      if (parent.lastIndex) child.lastIndex = parent.lastIndex;
    } else if (clone.__isDate(parent)) {
      child = new Date(parent.getTime());
    } else if (useBuffer && Buffer.isBuffer(parent)) {
      child = new Buffer(parent.length);
      parent.copy(child);
      return child;
    } else {
      if (typeof prototype == 'undefined') {
        proto = Object.getPrototypeOf(parent);
        child = Object.create(proto);
      }
      else {
        child = Object.create(prototype);
        proto = prototype;
      }
    }

    if (circular) {
      var index = allParents.indexOf(parent);

      if (index != -1) {
        return allChildren[index];
      }
      allParents.push(parent);
      allChildren.push(child);
    }

    for (var i in parent) {
      var attrs;
      if (proto) {
        attrs = Object.getOwnPropertyDescriptor(proto, i);
      }

      if (attrs && attrs.set == null) {
        continue;
      }
      child[i] = _clone(parent[i], depth - 1);
    }

    return child;
  }

  return _clone(parent, depth);
}

/**
 * Simple flat clone using prototype, accepts only objects, usefull for property
 * override on FLAT configuration object (no nested props).
 *
 * USE WITH CAUTION! This may not behave as you wish if you do not know how this
 * works.
 */

clone.clonePrototype = function clonePrototype(parent) {
  if (parent === null)
    return null;

  var c = function () {};
  c.prototype = parent;
  return new c();
};

// private utility functions

function __objToStr(o) {
  return Object.prototype.toString.call(o);
};
clone.__objToStr = __objToStr;

function __isDate(o) {
  return typeof o === 'object' && __objToStr(o) === '[object Date]';
};
clone.__isDate = __isDate;

function __isArray(o) {
  return typeof o === 'object' && __objToStr(o) === '[object Array]';
};
clone.__isArray = __isArray;

function __isRegExp(o) {
  return typeof o === 'object' && __objToStr(o) === '[object RegExp]';
};
clone.__isRegExp = __isRegExp;

function __getRegExpFlags(re) {
  var flags = '';
  if (re.global) flags += 'g';
  if (re.ignoreCase) flags += 'i';
  if (re.multiline) flags += 'm';
  return flags;
};
clone.__getRegExpFlags = __getRegExpFlags;

return clone;
})();

</script>
<script>
Benchmark.prototype.setup = function() {
    var oldObject = {"data":{"products":[{"type":"Product","data":{"id":13,"name":"Color Block Dress with Belt","title":"","description":"Vestido color block acinturado. Acompanha cinto de couro ecológico.","keyWords":"","Manufacturer":{"type":"Manufacturer","data":{"id":1,"manufacturer":"Fashion","logo":"/assets/img/brands/fashion.png"},"children":[]},"UoM":{"type":"UoM","data":{"id":1,"uom":"un."},"children":[]},"VariantOptions":[[{"type":"VariantOption","data":{"id":7,"variantOption":"Azul","idVariantGroup":2,"variantGroup":"Cor"},"children":[]},{"type":"VariantOption","data":{"id":9,"variantOption":"Marrom","idVariantGroup":2,"variantGroup":"Cor"},"children":[]}],[{"type":"VariantOption","data":{"id":4,"variantOption":"P","idVariantGroup":1,"variantGroup":"Tamanho"},"children":[]},{"type":"VariantOption","data":{"id":3,"variantOption":"M","idVariantGroup":1,"variantGroup":"Tamanho"},"children":[]},{"type":"VariantOption","data":{"id":2,"variantOption":"G","idVariantGroup":1,"variantGroup":"Tamanho"},"children":[]},{"type":"VariantOption","data":{"id":1,"variantOption":"GG","idVariantGroup":1,"variantGroup":"Tamanho"},"children":[]}]],"buyTogether":[],"crossSelling":[],"canonical":"/color-block-dress-with-belt.azul-p.html","showcase":{"principal":0,"images":[{"id":678,"src":"/assets/img/products/m/0387163445_1_1_1.jpg","alt":"","title":""},{"id":675,"src":"/assets/img/products/m/0387163445_2_2_1.jpg","alt":"","title":""},{"id":676,"src":"/assets/img/products/m/0387163445_2_3_1.jpg","alt":"","title":""},{"id":677,"src":"/assets/img/products/m/0387163445_2_4_1.jpg","alt":"","title":""}],"variants":[{"id":663,"src":"/assets/img/products/m/0387163445_1_1_1.jpg","alt":"","title":"","content":"","sample":"/assets/img/products/o/0387163445_1_1_1_sample.jpg"},{"id":679,"src":"/assets/img/products/m/7972156093_1_1_1.jpg","alt":"","title":"","content":"","sample":"/assets/img/products/o/7972156093_1_1_1_sample.jpg"}],"canonical":"/color-block-dress-with-belt.azul-p.html"},"mpn":"0387-P","variant":"Azul P","aggregateRating":{"reviews":0,"ratingValue":0},"installment":{"maxtimes":3,"price":100},"listprice":300,"ourprice":300,"availability":{"id":2,"presentation":"esgotado","itemAvailability":"SoldOut"},"status":{"id":1,"presentation":""},"qtAtCart":1},"children":[{"type":"AggregateRating","data":{"reviews":0,"ratingValue":0},"children":[]},{"type":"Detail","data":{"id":24,"content":"Vestido color block acinturado. Acompanha cinto de couro ecológico.","DetailSection":{"type":"DetailSection","data":{"id":1,"name":"Descrição","template":"","isContent":true},"children":[]}},"children":[]},{"type":"Detail","data":{"id":25,"content":"100% Polyester","DetailSection":{"type":"DetailSection","data":{"id":2,"name":"Composição","template":"","isContent":true},"children":[]}},"children":[]},{"type":"Variant","data":{"id":58,"isPrincipal":false,"codComp":"7;1;","sku":"0387-GG","width":15,"height":15,"depth":16,"weight":50,"name":"Azul GG"},"children":[{"type":"Picture","data":{"id":663,"isPrincipal":true,"name":"0387163445_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":659,"isPrincipal":false,"name":"0387163445_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":660,"isPrincipal":false,"name":"0387163445_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":661,"isPrincipal":false,"name":"0387163445_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":662,"isPrincipal":false,"name":"0387163445_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":1,"listprice":300,"ourprice":300,"maxtimes":3},"children":[]}]},{"type":"Variant","data":{"id":59,"isPrincipal":false,"codComp":"7;2;","sku":"0387-G","width":15,"height":15,"depth":15,"weight":50,"name":"Azul G"},"children":[{"type":"Picture","data":{"id":668,"isPrincipal":true,"name":"0387163445_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":664,"isPrincipal":false,"name":"0387163445_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":665,"isPrincipal":false,"name":"0387163445_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":666,"isPrincipal":false,"name":"0387163445_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":667,"isPrincipal":false,"name":"0387163445_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":1,"listprice":300,"ourprice":255,"maxtimes":3},"children":[]}]},{"type":"Variant","data":{"id":60,"isPrincipal":true,"codComp":"7;3;","sku":"0387-M","width":15,"height":15,"depth":16,"weight":50,"name":"Azul M"},"children":[{"type":"Picture","data":{"id":673,"isPrincipal":true,"name":"0387163445_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":669,"isPrincipal":false,"name":"0387163445_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":670,"isPrincipal":false,"name":"0387163445_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":671,"isPrincipal":false,"name":"0387163445_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":672,"isPrincipal":false,"name":"0387163445_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":1,"listprice":300,"ourprice":300,"maxtimes":3},"children":[]}]},{"type":"Variant","data":{"id":61,"isPrincipal":false,"codComp":"7;4;","sku":"0387-P","width":15,"height":15,"depth":15,"weight":50,"name":"Azul P"},"children":[{"type":"Picture","data":{"id":678,"isPrincipal":true,"name":"0387163445_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":674,"isPrincipal":false,"name":"0387163445_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":675,"isPrincipal":false,"name":"0387163445_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":676,"isPrincipal":false,"name":"0387163445_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":677,"isPrincipal":false,"name":"0387163445_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":1,"listprice":300,"ourprice":300,"maxtimes":3},"children":[]}]},{"type":"Variant","data":{"id":62,"isPrincipal":false,"codComp":"9;1;","sku":"7972-GG","width":15,"height":15,"depth":16,"weight":50,"name":"Marrom GG"},"children":[{"type":"Picture","data":{"id":679,"isPrincipal":true,"name":"7972156093_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":680,"isPrincipal":false,"name":"7972156093_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":681,"isPrincipal":false,"name":"7972156093_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":682,"isPrincipal":false,"name":"7972156093_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":683,"isPrincipal":false,"name":"7972156093_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":2,"listprice":300,"ourprice":255,"maxtimes":3},"children":[]}]},{"type":"Variant","data":{"id":63,"isPrincipal":false,"codComp":"9;2;","sku":"7972-G","width":15,"height":15,"depth":15,"weight":50,"name":"Marrom G"},"children":[{"type":"Picture","data":{"id":684,"isPrincipal":true,"name":"7972156093_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":685,"isPrincipal":false,"name":"7972156093_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":686,"isPrincipal":false,"name":"7972156093_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":687,"isPrincipal":false,"name":"7972156093_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":688,"isPrincipal":false,"name":"7972156093_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":2,"listprice":300,"ourprice":255,"maxtimes":3},"children":[]}]},{"type":"Variant","data":{"id":64,"isPrincipal":false,"codComp":"9;3;","sku":"7972-M","width":15,"height":15,"depth":15,"weight":50,"name":"Marrom M"},"children":[{"type":"Picture","data":{"id":689,"isPrincipal":true,"name":"7972156093_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":690,"isPrincipal":false,"name":"7972156093_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":691,"isPrincipal":false,"name":"7972156093_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":692,"isPrincipal":false,"name":"7972156093_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":693,"isPrincipal":false,"name":"7972156093_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":2,"listprice":300,"ourprice":255,"maxtimes":3},"children":[]}]},{"type":"Variant","data":{"id":65,"isPrincipal":false,"codComp":"9;4;","sku":"7972-P","width":15,"height":15,"depth":15,"weight":50,"name":"Marrom P"},"children":[{"type":"Picture","data":{"id":694,"isPrincipal":true,"name":"7972156093_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":695,"isPrincipal":false,"name":"7972156093_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":696,"isPrincipal":false,"name":"7972156093_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":697,"isPrincipal":false,"name":"7972156093_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":698,"isPrincipal":false,"name":"7972156093_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":2,"listprice":300,"ourprice":255,"maxtimes":3},"children":[]}]}]},{"type":"Product","data":{"id":13,"name":"Color Block Dress with Belt","title":"","description":"Vestido color block acinturado. Acompanha cinto de couro ecológico.","keyWords":"","Manufacturer":{"type":"Manufacturer","data":{"id":1,"manufacturer":"Fashion","logo":"/assets/img/brands/fashion.png"},"children":[]},"UoM":{"type":"UoM","data":{"id":1,"uom":"un."},"children":[]},"VariantOptions":[[{"type":"VariantOption","data":{"id":7,"variantOption":"Azul","idVariantGroup":2,"variantGroup":"Cor"},"children":[]},{"type":"VariantOption","data":{"id":9,"variantOption":"Marrom","idVariantGroup":2,"variantGroup":"Cor"},"children":[]}],[{"type":"VariantOption","data":{"id":4,"variantOption":"P","idVariantGroup":1,"variantGroup":"Tamanho"},"children":[]},{"type":"VariantOption","data":{"id":3,"variantOption":"M","idVariantGroup":1,"variantGroup":"Tamanho"},"children":[]},{"type":"VariantOption","data":{"id":2,"variantOption":"G","idVariantGroup":1,"variantGroup":"Tamanho"},"children":[]},{"type":"VariantOption","data":{"id":1,"variantOption":"GG","idVariantGroup":1,"variantGroup":"Tamanho"},"children":[]}]],"buyTogether":[],"crossSelling":[],"canonical":"/color-block-dress-with-belt.azul-m.html","showcase":{"principal":0,"images":[{"id":673,"src":"/assets/img/products/m/0387163445_1_1_1.jpg","alt":"","title":""},{"id":670,"src":"/assets/img/products/m/0387163445_2_2_1.jpg","alt":"","title":""},{"id":671,"src":"/assets/img/products/m/0387163445_2_3_1.jpg","alt":"","title":""},{"id":672,"src":"/assets/img/products/m/0387163445_2_4_1.jpg","alt":"","title":""}],"variants":[{"id":663,"src":"/assets/img/products/m/0387163445_1_1_1.jpg","alt":"","title":"","content":"","sample":"/assets/img/products/o/0387163445_1_1_1_sample.jpg"},{"id":679,"src":"/assets/img/products/m/7972156093_1_1_1.jpg","alt":"","title":"","content":"","sample":"/assets/img/products/o/7972156093_1_1_1_sample.jpg"}],"canonical":"/color-block-dress-with-belt.azul-m.html"},"mpn":"0387-M","variant":"Azul M","aggregateRating":{"reviews":0,"ratingValue":0},"installment":{"maxtimes":3,"price":100},"listprice":300,"ourprice":300,"availability":{"id":2,"presentation":"esgotado","itemAvailability":"SoldOut"},"status":{"id":1,"presentation":""},"qtAtCart":1},"children":[{"type":"AggregateRating","data":{"reviews":0,"ratingValue":0},"children":[]},{"type":"Detail","data":{"id":24,"content":"Vestido color block acinturado. Acompanha cinto de couro ecológico.","DetailSection":{"type":"DetailSection","data":{"id":1,"name":"Descrição","template":"","isContent":true},"children":[]}},"children":[]},{"type":"Detail","data":{"id":25,"content":"100% Polyester","DetailSection":{"type":"DetailSection","data":{"id":2,"name":"Composição","template":"","isContent":true},"children":[]}},"children":[]},{"type":"Variant","data":{"id":58,"isPrincipal":false,"codComp":"7;1;","sku":"0387-GG","width":15,"height":15,"depth":16,"weight":50,"name":"Azul GG"},"children":[{"type":"Picture","data":{"id":663,"isPrincipal":true,"name":"0387163445_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":659,"isPrincipal":false,"name":"0387163445_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":660,"isPrincipal":false,"name":"0387163445_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":661,"isPrincipal":false,"name":"0387163445_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":662,"isPrincipal":false,"name":"0387163445_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":1,"listprice":300,"ourprice":300,"maxtimes":3},"children":[]}]},{"type":"Variant","data":{"id":59,"isPrincipal":false,"codComp":"7;2;","sku":"0387-G","width":15,"height":15,"depth":15,"weight":50,"name":"Azul G"},"children":[{"type":"Picture","data":{"id":668,"isPrincipal":true,"name":"0387163445_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":664,"isPrincipal":false,"name":"0387163445_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":665,"isPrincipal":false,"name":"0387163445_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":666,"isPrincipal":false,"name":"0387163445_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":667,"isPrincipal":false,"name":"0387163445_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":1,"listprice":300,"ourprice":255,"maxtimes":3},"children":[]}]},{"type":"Variant","data":{"id":60,"isPrincipal":true,"codComp":"7;3;","sku":"0387-M","width":15,"height":15,"depth":16,"weight":50,"name":"Azul M"},"children":[{"type":"Picture","data":{"id":673,"isPrincipal":true,"name":"0387163445_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":669,"isPrincipal":false,"name":"0387163445_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":670,"isPrincipal":false,"name":"0387163445_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":671,"isPrincipal":false,"name":"0387163445_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":672,"isPrincipal":false,"name":"0387163445_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":1,"listprice":300,"ourprice":300,"maxtimes":3},"children":[]}]},{"type":"Variant","data":{"id":61,"isPrincipal":false,"codComp":"7;4;","sku":"0387-P","width":15,"height":15,"depth":15,"weight":50,"name":"Azul P"},"children":[{"type":"Picture","data":{"id":678,"isPrincipal":true,"name":"0387163445_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":674,"isPrincipal":false,"name":"0387163445_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":675,"isPrincipal":false,"name":"0387163445_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":676,"isPrincipal":false,"name":"0387163445_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":677,"isPrincipal":false,"name":"0387163445_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":1,"listprice":300,"ourprice":300,"maxtimes":3},"children":[]}]},{"type":"Variant","data":{"id":62,"isPrincipal":false,"codComp":"9;1;","sku":"7972-GG","width":15,"height":15,"depth":16,"weight":50,"name":"Marrom GG"},"children":[{"type":"Picture","data":{"id":679,"isPrincipal":true,"name":"7972156093_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":680,"isPrincipal":false,"name":"7972156093_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":681,"isPrincipal":false,"name":"7972156093_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":682,"isPrincipal":false,"name":"7972156093_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":683,"isPrincipal":false,"name":"7972156093_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":2,"listprice":300,"ourprice":255,"maxtimes":3},"children":[]}]},{"type":"Variant","data":{"id":63,"isPrincipal":false,"codComp":"9;2;","sku":"7972-G","width":15,"height":15,"depth":15,"weight":50,"name":"Marrom G"},"children":[{"type":"Picture","data":{"id":684,"isPrincipal":true,"name":"7972156093_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":685,"isPrincipal":false,"name":"7972156093_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":686,"isPrincipal":false,"name":"7972156093_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":687,"isPrincipal":false,"name":"7972156093_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":688,"isPrincipal":false,"name":"7972156093_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":2,"listprice":300,"ourprice":255,"maxtimes":3},"children":[]}]},{"type":"Variant","data":{"id":64,"isPrincipal":false,"codComp":"9;3;","sku":"7972-M","width":15,"height":15,"depth":15,"weight":50,"name":"Marrom M"},"children":[{"type":"Picture","data":{"id":689,"isPrincipal":true,"name":"7972156093_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":690,"isPrincipal":false,"name":"7972156093_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":691,"isPrincipal":false,"name":"7972156093_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":692,"isPrincipal":false,"name":"7972156093_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":693,"isPrincipal":false,"name":"7972156093_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":2,"listprice":300,"ourprice":255,"maxtimes":3},"children":[]}]},{"type":"Variant","data":{"id":65,"isPrincipal":false,"codComp":"9;4;","sku":"7972-P","width":15,"height":15,"depth":15,"weight":50,"name":"Marrom P"},"children":[{"type":"Picture","data":{"id":694,"isPrincipal":true,"name":"7972156093_1_1_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":695,"isPrincipal":false,"name":"7972156093_1_1_1_sample","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":696,"isPrincipal":false,"name":"7972156093_2_2_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":697,"isPrincipal":false,"name":"7972156093_2_3_1","alt":"","relatedTo":0},"children":[]},{"type":"Picture","data":{"id":698,"isPrincipal":false,"name":"7972156093_2_4_1","alt":"","relatedTo":0},"children":[]},{"type":"Price","data":{"idAvailability":2,"idStatus":2,"listprice":300,"ourprice":255,"maxtimes":3},"children":[]}]}]}]}};
     
     
      function deepCopy(value) {
        if (value instanceof Date) return new Date(value);
        if (typeof value === 'object') {
          if (value === null) return null;
          if (Array.isArray(value)) {
            var array = [];
            for (var i = value.length; i--;) {
              array[i] = deepCopy(value[i]);
            }
            return array;
          }
          var object = new value.constructor();
          for (var key in value) {
            if (value.hasOwnProperty(key)) {
              object[key] = deepCopy(value[key]);
            }
          }
          return object;
        }
        return value;
      }
   
    (function(){
        var hop = Object.prototype.hasOwnProperty;
        cloneOf = function(x){
            switch(typeof x) {
                case "object": return cloneOfObj(x);
                default: return x;
            }
        }
        var cloneOfArr = function(a){
            var i = a.length, c = new Array(i);
            while(i--)c[i] = cloneOf(a[i]);
            return c;
        }
        var cloneOfObj = function(o){
            if(!o) return null;
            if(Array.isArray(o)) return cloneOfArr(o);
            var c = {};
            for(var key in o) if(hop.call(o,key)) c[key] = cloneOf(o[key]);
            return c;
        }
    }());
};
</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
jQuery.extend() deep
var newObject = jQuery.extend(true, {}, oldObject);
pending…
JSON stringify/parse
var newObject = JSON.parse(JSON.stringify(oldObject));
pending…
immutable.js
var newObject = Immutable.fromJS(oldObject).toObject();
pending…
cloneOf
var newObject = cloneOf(oldObject);
pending…
deepCopy
var newObject = deepCopy(oldObject);
pending…
node-clone
var newObject = clone(oldObject);
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