Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36
Chrome 101
Windows
Desktop
2 years ago
Test name Executions per second
lodash cloneDeep 690432.8 Ops/sec
JSON.parse(JSON.stringify()) 433964.4 Ops/sec
recursiveDeepCopy 768139.4 Ops/sec
fastest-json-copy npm package 530689.4 Ops/sec
jsondiffpatch.clone() 2053098.8 Ops/sec
rfdc 2098282.5 Ops/sec
HTML Preparation code:
AخA
 
1
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js'></script>
2
<script src='https://cdn.jsdelivr.net/npm/jsondiffpatch@0.4.1/dist/jsondiffpatch.umd.min.js'></script>
3
<script src='https://cdn.jsdelivr.net/npm/rfdc@1.3.0'></script>
Script Preparation code:
x
 
var MyObject = {
  description: 'Creates a deep copy of source, which should be an object or an array.',
  myNumber: 123456789,
  myBoolean: true,
  jayson: {
    stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
    parse: 'JSON.parse() method parses a JSON string...'
  }
};
var myCopy = null;
var jsondiffpatchClone = jsondiffpatch.clone;
var rfdcClone = rfdc();
///////////////////////////////////////////////////////////////////
function recursiveDeepCopy(o) {
    var newO,
      i;
  
    if (typeof o !== 'object') {
      return o;
    }
    if (!o) {
      return o;
    }
  
    if ('[object Array]' === Object.prototype.toString.apply(o)) {
      newO = [];
      for (i = 0; i < o.length; i += 1) {
        newO[i] = recursiveDeepCopy(o[i]);
      }
      return newO;
    }
  
    newO = {};
    for (i in o) {
      if (o.hasOwnProperty(i)) {
        newO[i] = recursiveDeepCopy(o[i]);
      }
    }
    return newO;
  }
//////////////////////////////////////////////////////////////////
// Use https://www.npmjs.com/package/fastest-json-copy
// Instead of the code below! It is the same thing, just minified.
var isArray=Array.isArray;function fastestJsonCopy(r){if(!r)return r;if(isArray(r)){for(var e=[],o=r.length,t=0;t<o;t++)e.push(fastestJsonCopy(r[t]));return e}if("object"==typeof r){var y=Object.keys(r),a=(o=y.length,{});for(t=0;t<o;t++){var c=y[t];a[c]=fastestJsonCopy(r[c])}return a}return r}
Tests:
  • lodash cloneDeep

     
    myCopy = _.cloneDeep(MyObject);
  • JSON.parse(JSON.stringify())

     
    myCopy = JSON.parse(JSON.stringify(MyObject));
  • recursiveDeepCopy

     
    myCopy = recursiveDeepCopy(MyObject);
  • fastest-json-copy npm package

     
    myCopy = fastestJsonCopy(MyObject);
  • jsondiffpatch.clone()

     
    myCopy = jsondiffpatchClone(MyObject);
  • rfdc

     
    myCopy = rfdcClone(MyObject);