<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
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....',
x: {
description: 'Creates a deep copy of source, which should be an object or an array.',
myNumber: 123456789,
myBoolean: true,
bsae64String: 'abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc123',
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses 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;
}
myCopy = _.cloneDeep(MyObject);
myCopy = JSON.parse(JSON.stringify(MyObject));
myCopy = recursiveDeepCopy(MyObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash CloneDeep | |
Json Clone | |
recursiveDeepCopy |
Test name | Executions per second |
---|---|
Lodash CloneDeep | 699873.0 Ops/sec |
Json Clone | 383450.9 Ops/sec |
recursiveDeepCopy | 4960478.5 Ops/sec |
I'd be happy to explain what's being tested in the provided benchmark.
The test aims to compare the performance of three different methods for creating a deep copy of an object:
cloneDeep
: This method uses a recursive approach to create a deep copy of an object.JSON.parse(JSON.stringify())
: This method uses the Stringify()
and Parse()
methods of the JSON object to create a deep copy of an object.recursiveDeepCopy
: This is a custom implementation of a recursive function that creates a deep copy of an object.Options Comparison:
cloneDeep
: This method is well-tested and optimized for deep copying objects. However, it may not be suitable for very large objects due to memory constraints.JSON.parse(JSON.stringify())
: This method can work with any JSON-serializable object, but it's not the most efficient way to create a deep copy, especially for complex objects.recursiveDeepCopy
: This custom implementation provides more control over the copying process, but it may require more code and maintenance.Pros and Cons:
cloneDeep
:JSON.parse(JSON.stringify())
:recursiveDeepCopy
:Other Considerations:
Alternative Methods:
Some other alternatives for creating deep copies of objects include:
Object.assign()
.Keep in mind that these alternatives may not provide the same level of performance or control as Lodash's cloneDeep
method, and some might require more code and maintenance.