<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...'
}
};
var myCopy = null;
myCopy = _.cloneDeep(MyObject);
myCopy = JSON.stringify(MyObject);
mycopy = structuredClone(MyObject)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Json stringify | |
structuredClone |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 1367467.1 Ops/sec |
Json stringify | 1109203.5 Ops/sec |
structuredClone | 534746.8 Ops/sec |
Let's break down the test cases and compare the options used.
Test Case 1: Lodash cloneDeep
The benchmark tests the performance of _.cloneDeep()
from Lodash, which creates a deep copy of an object. The function takes an object as input and returns a new object that is a copy of the original object, including all nested objects and arrays.
Pros:
.cloneDeep()
is a specialized function that only clones the specified object, making it more efficient than creating a full-fledged copy.Cons:
Test Case 2: JSON stringify
The benchmark tests the performance of JSON.stringify()
which converts a JavaScript value to a JSON string. This is done by taking an object as input and returning a string representation of that object, which can be useful for logging or serialization purposes.
Pros:
JSON.stringify()
is a built-in function in JavaScript, eliminating the need for external libraries.Cons:
Test Case 3: structuredClone
The benchmark tests the performance of structuredClone()
which creates a deep copy of an object, similar to .cloneDeep()
. However, it is designed for use with WebAssembly (WASM) and provides additional features like handling cyclic references.
Pros:
.cloneDeep()
above.JSON.stringify()
.Cons:
.cloneDeep()
, it's an external library that needs to be included in the test environment, adding overhead.Comparison
All three options have their trade-offs. If you need a deep copy of an object with nested properties and want to preserve data structures, _.cloneDeep()
(Lodash) might be the best choice. However, it comes with the overhead of including an external library.
JSON.stringify()
, on the other hand, provides a lightweight solution that converts objects to strings but loses their original data structure and nesting. It's suitable for logging or serialization purposes where the resulting string is more important than the original object.
For use cases involving WASM, structuredClone()
offers better support for cyclic references compared to .cloneDeep()
. However, it also requires an external library and might not be as widely adopted or well-supported outside of this specific context.
Other Alternatives
Object.assign()
or spread operator ({...obj}
) to create a shallow copy of an object. However, these methods may not work correctly for nested objects or cyclic references.In summary, the choice between .cloneDeep()
, JSON.stringify()
, and structuredClone()
depends on your specific requirements, including the need for preserving data structures, handling cyclic references, and performance considerations.