Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON.stringify vs structuredClone vs simple deepCopyObject
(version: 0)
JSON.stringify vs structuredClone vs simple deepCopyObject
Comparing performance of:
JSON.stringify vs structuredClone vs deepCopyObject
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var deepCopyObject = (inObject) => { if (inObject instanceof Date) return inObject if (inObject instanceof Set) return new Set(inObject) if (inObject instanceof Map) return new Map(inObject) if (typeof inObject !== 'object' || inObject === null) { return inObject // Return the value if inObject is not an object } // Create an array or object to hold the values const outObject = Array.isArray(inObject) ? [] : {} Object.keys(inObject).forEach((key) => { const value = inObject[key] // Recursively (deep) copy for nested objects, including arrays outObject[key] = typeof value === 'object' && value !== null ? deepCopyObject(value) : value }) return outObject } 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;
Tests:
JSON.stringify
myCopy = JSON.parse(JSON.stringify(MyObject));
structuredClone
myCopy = structuredClone(MyObject);
deepCopyObject
myCopy = deepCopyObject(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
JSON.stringify
structuredClone
deepCopyObject
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON.stringify
1497551.0 Ops/sec
structuredClone
856547.8 Ops/sec
deepCopyObject
1326036.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript benchmarking test case, specifically designed to compare the performance of three different approaches for creating a deep copy of an object: `JSON.stringify`, `structuredClone`, and `deepCopyObject`. **Options Compared** 1. **JSON.stringify**: This method converts a JavaScript value to a JSON string and then parses it back into a JavaScript object. While it can create a shallow copy of objects, it may not work correctly for nested objects or arrays. 2. **structuredClone**: This is a new JavaScript feature introduced in ECMAScript 2020, which creates a deep copy of an object without serializing its value to a string. It's designed to be more efficient and accurate than `JSON.stringify`. 3. **deepCopyObject**: This custom implementation provides a recursive function that creates a deep copy of an object by iterating over its properties and recursively copying nested objects. **Pros and Cons** * **JSON.stringify**: * Pros: Simple and widely supported. * Cons: May not work correctly for nested objects or arrays, can lead to memory leaks due to circular references. * **structuredClone**: * Pros: New JavaScript feature, designed for deep copying without serialization, more efficient than `JSON.stringify`. * Cons: Requires modern browsers and Node.js versions (14.13.0 or later). * **deepCopyObject**: * Pros: Custom implementation provides full control over the copy process, handles nested objects correctly. * Cons: More complex to implement, may not be as efficient as `structuredClone` for large datasets. **Library and Special JavaScript Features** The test case uses the `structuredClone` function, which is a built-in JavaScript feature. It also uses the custom implementation of `deepCopyObject`, but no libraries are mentioned in the provided code. **Special JavaScript Feature** No special JavaScript features are used beyond the introduction of `structuredClone`. **Other Alternatives** If you need to create a deep copy of an object without using `JSON.stringify` or `structuredClone`, other alternatives include: * **Lodash's `cloneDeep()`**: A popular utility library that provides a `cloneDeep()` function for creating deep copies. * **Underscore.js's `_cloneDeep()`**: Another utility library that offers a similar functionality to Lodash. * **Custom implementations**: You can also create your own recursive function to iterate over an object's properties and create a deep copy. For the use case mentioned in the benchmark, `structuredClone` is recommended due to its efficiency and accuracy. However, for more complex or older JavaScript environments, the custom implementation of `deepCopyObject` may be necessary.
Related benchmarks:
JSON.stringify vs structuredClone vs simple deepCopyObject 2
JSON.stringify vs structuredClone vs simple deepCopyObject 3
JSON.stringify vs structuredClone vs simple deepCopyObject 4
JSON.stringify vs structuredClone vs simple deepCopyObject 5
Comments
Confirm delete:
Do you really want to delete benchmark?