Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON.stringify vs structuredClone (with deeply nested objects)
(version: 0)
JSON.stringify vs structuredClone
Comparing performance of:
JSON.stringify vs structuredClone
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
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...' }, deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {deep: {}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}} }; var myCopy = null;
Tests:
JSON.stringify
myCopy = JSON.parse(JSON.stringify(MyObject));
structuredClone
myCopy = structuredClone(MyObject);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
JSON.stringify
structuredClone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON.stringify
278575.9 Ops/sec
structuredClone
115386.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided benchmark compares two JavaScript methods for creating copies of objects: `JSON.stringify` and `structuredClone`. The benchmark measures which method is faster and more efficient. **Options Compared** Two options are compared: 1. **JSON.stringify**: This method converts a JavaScript object to a JSON string, which can be easily parsed back into an object using `JSON.parse()`. 2. **structuredClone**: This method creates a deep copy of an object without converting it to a JSON string. It is designed to work with objects that contain cyclic references (i.e., objects that reference themselves) and is considered more efficient than `JSON.stringify` for large objects. **Pros and Cons** * **JSON.stringify**: + Pros: Simple and widely supported, can be used for serializing data. + Cons: Can create a new object with different properties and methods, which may not be desirable for all use cases. Also, it converts the entire object to a JSON string, including any cyclic references, which can lead to a large amount of memory usage. * **structuredClone**: + Pros: Creates an exact copy of the original object, including all its properties and methods, without converting it to a JSON string. It is more efficient for large objects and can handle cyclic references. + Cons: Less widely supported than `JSON.stringify`, may require additional dependencies or libraries. **Library Used** * **structuredClone**: This method is part of the ECMAScript standard since ECMAScript 2020 (ES10). However, it was not widely supported until recently. Mezzanine (https://github.com/Mezzanine-Team/mezzanine) and V8 (https://v8.dev/) are two notable implementations that have contributed to its adoption. **Special JavaScript Feature/Syntax** * **Cyclic References**: The benchmark creates objects with cyclic references, which is a common challenge when serializing data. `structuredClone` is designed to handle these cases efficiently. * **Object Deep Copying**: Both methods create deep copies of the original object, but `structuredClone` does so without converting it to a JSON string. **Other Alternatives** If you need to compare other serialization techniques, here are some alternatives: 1. **Lodash.cloneDeep()**: A popular library that provides a deep copy function for objects. 2. **immer**: A library that provides a way to create immutable objects by creating a shallow copy and then modifying the copy. 3. **JSON.parse(JSON.stringify(object))**: While not as efficient as `structuredClone`, this method can still be used for simple object copying. In summary, the benchmark compares two popular methods for serializing JavaScript objects: `JSON.stringify` and `structuredClone`. While `JSON.stringify` is widely supported and simple to use, it has limitations when dealing with cyclic references or large objects. `structuredClone` provides a more efficient solution but is less widely adopted.
Related benchmarks:
Lodash cloneDeep vs structuredClone vs JSON Parse (deep object)
JSON.stringify vs structuredClone 2
Lodash cloneDeep vs structuredClone vs JSON-JSON
Lodash cloneDeep vs structuredClone vs JSON.parse + JSON.stringify but with big data
Comments
Confirm delete:
Do you really want to delete benchmark?