DeepCopy

Benchmark created by BrandonLWhite on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

<script>
window.global = window;
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(e.fastCopy={})}(this,function(e){"use strict";var b="string"==typeof/foo/g.flags,l="function"==typeof global.Object.getOwnPropertySymbols,t="function"==typeof global.WeakSet,d=Object.create,p=Object.keys,y=Object.getOwnPropertySymbols,g=Object.prototype.propertyIsEnumerable,h=function(e){return e.slice(0)},n=function(f){return function(e,t,n){var r=new e.constructor;return e.forEach(f(r,t,n)),r}},v=n(function(n,r,f){return function(e,t){return n.set(t,r(e,f))}}),w=n(function(t,n,r){return function(e){return t.add(n(e,r))}}),O=function(e,t,n,r){var f=r?{}:e.constructor?new e.constructor:d(null),o=p(e);if(o.length)for(var u=void 0,a=0;a<o.length;a++)f[u=o[a]]=t(e[u],n);if(l){var i=y(e);if(i.length)for(var c=void 0,s=0;s<i.length;s++)c=i[s],g.call(e,c)&&(f[c]=t(e[c],n))}return f};e.default=function(e){var y=1<arguments.length&&void 0!==arguments[1]?arguments[1]:global,g=t?new WeakSet:d({_values:[],add:function(e){this._values.push(e)},has:function(e){return!!~this._values.indexOf(e)}});return function e(t){if(r=g,"object"!=typeof(n=t)||null===n||r.has(n))return t;var n,r,f,o,u,a,i,c,s,l,d,p;if(Array.isArray(t))return g.add(t),function(e,t,n){for(var r=new e.constructor,f=0;f<e.length;f++)r[f]=t(e[f],n);return r}(t,e,y);if(t.constructor===y.Object)return g.add(t),O(t,e,y,!0);if(t instanceof y.Date)return new Date(t.getTime());if(t instanceof y.RegExp)return f=t,(a=new y.RegExp(f.source,b?f.flags:(u="",(o=f).global&&(u+="g"),o.ignoreCase&&(u+="i"),o.multiline&&(u+="m"),o.unicode&&(u+="u"),o.sticky&&(u+="y"),u))).lastIndex=f.lastIndex,a;if(y.Map&&t instanceof y.Map)return g.add(t),v(t,e,y);if(y.Set&&t instanceof y.Set)return g.add(t),w(t,e,y);if(y.Buffer&&y.Buffer.isBuffer(t))return i=t,s=(c=y).Buffer.allocUnsafe?c.Buffer.allocUnsafe(i.length):new c.Buffer(i.length),i.copy(s),s;if(y.ArrayBuffer){if(y.ArrayBuffer.isView(t))return new(l=t).constructor(h(l.buffer));if(t instanceof y.ArrayBuffer)return h(t)}return p=y,"function"==typeof(d=t).then||d instanceof p.Error||p.WeakMap&&d instanceof p.WeakMap||p.WeakSet&&d instanceof p.WeakSet?t:(g.add(t),O(t,e,y))}(e)},Object.defineProperty(e,"__esModule",{value:!0})});
</script>

Setup

var source = JSON.parse(`
    {
      "_id": "5bc0c21ba0cac69d1bc8aadb",
      "index": 0,
      "guid": "6ba42b57-4ce4-4822-be38-3f26ce58ec36",
      "isActive": true,
      "balance": "$1,604.29",
      "picture": "http://placehold.it/32x32",
      "age": 35,
      "eyeColor": "green",
      "name": {
        "first": "Deanne",
        "last": "Hooper"
      },
      "company": "VIXO",
      "email": "deanne.hooper@vixo.biz",
      "phone": "+1 (934) 418-3885",
      "address": "167 Bedell Lane, Taycheedah, Louisiana, 2329",
      "about": "Id mollit magna aute nulla nulla cupidatat sint ea esse. Dolor sint do eu esse ipsum. Ullamco quis aute incididunt cillum officia ea aute culpa deserunt. Incididunt aliquip aliqua amet minim dolor quis consectetur magna magna laboris deserunt. Ad Lorem exercitation aute et aliqua elit nulla duis. Elit laborum mollit non ea qui anim. Do id veniam consectetur excepteur ex.",
      "registered": "Friday, May 20, 2016 11:37 AM",
      "latitude": "-12.171509",
      "longitude": "-74.126673",
      "tags": [
        "sint",
        "culpa",
        "amet",
        "laboris",
        "ad"
      ],
      "range": [
        0,
        1,
        2,
        3,
        4,
        5,
        6,
        7,
        8,
        9
      ],
      "friends": [
        {
          "id": 0,
          "name": "Camille Pate"
        },
        {
          "id": 1,
          "name": "Hines Zamora"
        },
        {
          "id": 2,
          "name": "Maxine Jensen"
        }
      ],
      "greeting": "Hello, Deanne! You have 8 unread messages.",
      "favoriteFruit": "strawberry"
    }
  `);

Test runner

Ready to run.

Testing in
TestOps/sec
Lodash cloneDeep()
_.cloneDeep(largeObject);
ready
JSON roundtrip
JSON.parse(JSON.stringify(largeObject));
ready
fast-copy
fastCopy.default(largeObject);
ready

Revisions

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

  • Revision 1: published by BrandonLWhite on