Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON.stringify vs structuredClone
(version: 0)
JSON.stringify vs structuredClone
Comparing performance of:
JSON.stringify vs structuredClone
Created:
4 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...' } }; 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:
13 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON.stringify
2269550.2 Ops/sec
structuredClone
997598.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its results. **Benchmark Definition** The test creates two JavaScript microbenchmarks that compare the performance of `JSON.stringify` and `structuredClone`. The goal is to determine which method is faster for creating a deep copy of an object. **Script Preparation Code** The script preparation code defines a JavaScript object `MyObject` with various properties, including `description`, `myNumber`, `myBoolean`, and `jayson`. The `jayson` property contains another object with two properties: `stringify` and `parse`. **Html Preparation Code** There is no HTML preparation code provided. **Test Cases** The benchmark consists of two test cases: 1. **JSON.stringify**: Creates a deep copy of `MyObject` using `JSON.parse(JSON.stringify(MyObject))`. 2. **structuredClone**: Creates a deep copy of `MyObject` using `structuredClone(MyObject)`. **Library and its Purpose** In this benchmark, the `structuredClone` library is used to create a deep copy of the object. The `structuredClone` function was introduced in ECMAScript 2020 as a replacement for `JSON.parse(JSON.stringify())`. It provides a safer way to clone objects, as it can handle cyclic references and other edge cases. **Special JS Feature or Syntax** There is no special JavaScript feature or syntax used in this benchmark. The only notable aspect is the use of `structuredClone`, which is a relatively new function introduced in ECMAScript 2020. **Pros and Cons of Different Approaches** The two approaches are: * **JSON.stringify**: This method has been widely used for years, but it has some limitations: + It can throw errors if the object contains cyclic references or other invalid data. + It can be slower than `structuredClone` due to its parsing steps. * **structuredClone**: This method is safer and faster than `JSON.stringify`, as it avoids parsing and creates a deep copy directly: + It handles cyclic references and other edge cases more robustly. + It is generally faster than `JSON.stringify`. **Other Alternatives** Before the introduction of `structuredClone`, developers used to use `JSON.parse(JSON.stringify())` or third-party libraries like Lodash's `cloneDeep()` function. Both approaches had their limitations, but `structuredClone` has become the recommended way to create deep copies in modern JavaScript. In summary, the benchmark shows that `structuredClone` is generally faster and safer than `JSON.stringify` for creating deep copies of objects. This is a significant improvement over the older approach, making it easier to write robust and efficient code.
Related benchmarks:
Lodash cloneDeep vs structuredClone vs JSON Parse (deep object)
Lodash cloneDeep vs structuredClone vs JSON-JSON
Lodash cloneDeep vs structuredClone vs JSON.parse + JSON.stringify but with big data
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?