Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON.stringify vs structuredClone, modifications
(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: 10, 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
MyObject.myNumber++; myCopy = JSON.parse(JSON.stringify(MyObject));
structuredClone
MyObject.myNumber++; 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
1763562.6 Ops/sec
structuredClone
772809.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Overview** The benchmark compares two approaches to create a copy of an object: `JSON.stringify` and `structuredClone`. The test case uses a sample object, `MyObject`, which contains nested properties, including a `jayson` property that references another string. The goal is to determine which approach is faster and more efficient. **Options Compared** The two options being compared are: 1. **JSON.stringify** 2. **structuredClone** **Pros and Cons of each approach:** **JSON.stringify** * Pros: + Widely supported in most JavaScript engines. + Simple to implement. * Cons: + Can be slower than `structuredClone` for large objects due to the overhead of serializing and parsing JSON strings. + Can lead to memory leaks if not handled correctly (e.g., using `JSON.parse()` on a string that was previously serialized). **structuredClone** * Pros: + Designed specifically for creating deep copies of objects, making it faster and more efficient than `JSON.stringify`. + Reduces the risk of memory leaks by only cloning the object's properties. * Cons: + Requires support from modern JavaScript engines (e.g., Chrome 119 and later). + May not be supported in older browsers or environments. **Library/Functionality Used** In this benchmark, `structuredClone` is used to create a deep copy of the `MyObject`. `structuredClone` is a relatively new function introduced in ECMAScript 2020 (ES2020) as part of the "Object Spread" syntax. It's designed to create a new object with the same properties as an existing one, without serializing or deserializing data. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes being used in this benchmark. However, it's worth noting that `structuredClone` is a relatively new feature that may not be supported in all browsers or environments yet. **Other Alternatives** If you need to create a copy of an object without using `structuredClone`, other alternatives include: 1. **Object.assign()**: Can be used to spread properties from one object to another, but it only copies shallowly. 2. **Lodash's `cloneDeep()` function**: A popular utility library that provides a deep cloning function for objects. 3. **Manual implementation using recursion or iteration**: You can implement your own deep copying logic using recursive functions or iterations. Keep in mind that each alternative has its own trade-offs and may not be as efficient or reliable as `structuredClone`.
Related benchmarks:
Lodash cloneDeep vs structuredClone vs JSON Parse (deep object)
Lodash cloneDeep vs structuredClone vs JSON-JSON
lodash clonedeep vs json.parse(stringify()) vs structuredClone
Lodash cloneDeep vs structuredClone vs JSON Parse (100 000 objects)
lodash clonedeep vs json.parse(stringify()) vs recursivecopy heavy
Comments
Confirm delete:
Do you really want to delete benchmark?