Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36
Chrome 116
Mac OS X 10.15.7
Desktop
one year ago
Test name Executions per second
Lodash CloneDeep 98834.8 Ops/sec
Json Clone 73575.1 Ops/sec
recursiveDeepCopy 137681.9 Ops/sec
HTML Preparation code:
AخA
 
1
<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js'></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...',
    test: {
      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...',
        test: {
          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...',
            test: {
              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...',
                test: {
                  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...',
                    test: {
                      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...',
                        test: {
                          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...',
                            test: {
                              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...',
                                test: {
                                  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...',
                                    test: {
                                      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...',
                                        test: {
                                          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...',
                                            test: {
                                              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...',
                                                test: {
                                                  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...',
                                                    test: {
                                                      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...',
                                                        test: {
                                                          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...',
                                                            test: {
                                                              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;
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;
  }
Tests:
  • Lodash CloneDeep

     
    myCopy = _.cloneDeep(MyObject);
  • Json Clone

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

     
    myCopy = recursiveDeepCopy(MyObject);