<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....',
parse: 'JSON.parse() method parses a JSON string...'
},
nesting: {
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...'
},
nesting: {
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...'
},
nesting: {
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;
myCopy = _.cloneDeep(MyObject);
myCopy = structuredClone(MyObject);
myCopy = JSON.parse(JSON.stringify(MyObject));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Native structuredClone | |
JSON.stringify(JSON.parse()) |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 584812.8 Ops/sec |
Native structuredClone | 394425.9 Ops/sec |
JSON.stringify(JSON.parse()) | 593983.3 Ops/sec |
The provided JSON represents a JavaScript benchmark test case for comparing the performance of three different methods: Lodash's cloneDeep
, the native structuredClone
API, and JSON.stringify(JSON.parse())
.
Test Cases
cloneDeep
function to create a deep copy of the input object MyObject
. The cloneDeep
function recursively clones all properties of the object, including nested objects and arrays.structuredClone
API (introduced in ECMAScript 2022) to create a deep copy of the input object MyObject
. The structuredClone
function is specifically designed for cloning complex data structures and can handle cycles more efficiently than other methods.JSON.stringify()
and JSON.parse()
to create a deep copy of the input object MyObject
. First, JSON.stringify()
converts the object into a JSON string, and then JSON.parse()
parses that string back into an object.Comparison
The pros and cons of each approach are:
structuredClone
structuredClone
due to the overhead of converting to a string and back to an objectOther Considerations
structuredClone
can handle cycles more efficiently by recursively cloning only the top-level object.JSON.stringify(JSON.parse())
assumes that the input object is safe to serialize and deserialize. If the object contains cycles or other problematic data structures, this method may fail.Alternative Methods
If none of these methods are suitable for your use case, you can also consider using:
Object.assign()
with a custom clone function: This method can be used to create a deep copy of an object by recursively cloning its properties.Array.prototype.map()
: This method can be used to create a deep copy of an array or object by recursively cloning its elements.However, keep in mind that these alternative methods may not be as efficient or widely supported as the methods mentioned above.