Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Chrome 120
Mac OS X 10.15.7
Desktop
one year ago
Test name Executions per second
Lodash 137415.1 Ops/sec
structuredClone 92222.6 Ops/sec
recursiveDeepCopy 152989.3 Ops/sec
Json clone 99009.4 Ops/sec
HTML Preparation code:
AخA
 
1
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
x
 
var MySimpleObject = {
    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 MyDeepObject = {
    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...',
        object: {
            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...',
                object: {
                    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...',
                        object: {
                            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...',
                                object: {
                                    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...',
                                        object: {
                                            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...',
                                                something: [0, 1, 2, 3, 3, 4, 4, 5, 5, 5, 3, 24, 234, 2, 34, 23, 4, 23, 4, 234],
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
};
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

     
    myCopy = _.cloneDeep(MySimpleObject);
    myCopy = _.cloneDeep(MyDeepObject);
  • structuredClone

     
    myCopy = structuredClone(MySimpleObject);
    myCopy = structuredClone(MyDeepObject);
  • recursiveDeepCopy

     
    myCopy = recursiveDeepCopy(MySimpleObject);
    myCopy = recursiveDeepCopy(MyDeepObject);
  • Json clone

     
    myCopy = JSON.parse(JSON.stringify(MySimpleObject));
    myCopy = JSON.parse(JSON.stringify(MyDeepObject));