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 = JSON.parse(structuredClone(JSON.stringify(MyObject)));
myCopy = structuredClone(MyObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
JSON.stringify | |
structuredClone |
Test name | Executions per second |
---|---|
JSON.stringify | 810585.6 Ops/sec |
structuredClone | 710809.8 Ops/sec |
The benchmark you're looking at evaluates the performance of two different methods for creating a deep copy of a JavaScript object: using JSON.stringify
in combination with JSON.parse
, and using the built-in structuredClone
method. Below, I will explain the options being compared, their pros and cons, and other considerations that may be relevant for software engineers.
JSON.stringify
+ JSON.parse
:
myCopy = JSON.parse(structuredClone(JSON.stringify(MyObject)));
JSON.stringify
structuredClone
:
myCopy = structuredClone(MyObject);
structuredClone
Pros:
Cons:
structuredClone
, especially for large objects, due to the overhead of converting to a string and then parsing it.undefined
, Symbol
, and other non-serializable values are not included.Pros:
Cons:
structuredClone
is becoming more widely supported, it may not be available in all environments, particularly older browsers or less common platforms.Context of Use: Choosing between these methods will depend on your particular use case. If you need to deep clone objects that contain non-serializable properties or want to maintain their original data types, structuredClone
would be the recommended approach. However, for simple objects that are JSON-serializable, the JSON.stringify
+ JSON.parse
approach might be sufficient.
Performance Results: According to the benchmark results, JSON.stringify
achieved 810,585.56 executions per second, while structuredClone
achieved 710,809.81 executions per second. This suggests that, at least in this test case and environment, JSON.stringify
is slightly quicker, though this may vary widely with different object sizes and complexities.
Manual Deep Copying: For specific use cases, especially for simpler objects, developers might opt to manually create a deep copy by recursively copying properties. This can give full control over what gets copied but requires a proper implementation to handle various data types and edge cases.
Third-Party Libraries: Libraries like Lodash provide deep cloning functionality (e.g., _.cloneDeep()
). They handle more edge cases and provide a well-tested implementation, but they add an additional dependency to your project.
In summary, selecting the appropriate method for object cloning depends on the context, the complexity of the objects being cloned, performance considerations, and compatibility needs. The two methods compared in this benchmark have clear tradeoffs, and understanding these will help you make informed decisions in your projects.