Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON.parse + JSON.stringify vs structuredClone (cyclical graph)
(version: 0)
Comparing performance of:
JSON vs structuredClone
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { cycle: { cycle: { cycle: { cycle: { cycle: { cycle: { cycle: { cycle: 1 } } } } } } } }
Tests:
JSON
JSON.parse(JSON.stringify(obj));
structuredClone
structuredClone(obj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
JSON
structuredClone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON
772869.6 Ops/sec
structuredClone
187302.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark defines two different approaches to compare: 1. `JSON.parse(JSON.stringify(obj));` 2. `structuredClone(obj);` These two methods are used to serialize and deserialize an object, specifically a complex cyclic graph represented by the JSON string in the "Script Preparation Code". **What is being tested?** In this case, we're testing how efficient each method is at handling cyclic graphs, which can occur when objects reference themselves indirectly. **Options compared** The two methods are: 1. `JSON.parse(JSON.stringify(obj));`: This method uses the JSON Stringify function to convert the object to a string, and then parses it back using JSON.parse() with the same options as before (e.g., null as the replacer function). The replacer function is not specified, which means that any circular references will be converted to `[Circular]` strings. 2. `structuredClone(obj);`: This method uses the `structuredClone()` function introduced in ECMAScript 2020, which provides a more efficient and safer way to create deep copies of objects while handling cyclic graphs. **Pros and Cons** 1. **JSON.parse(JSON.stringify(obj));** * Pros: Wide browser support (most modern browsers), simple implementation. * Cons: + Not designed for handling cyclic graphs and can produce incorrect results or crash the engine when encountering self-referential cycles. + Requires a replacer function to handle circular references, which can be tricky to implement correctly. 2. **structuredClone(obj);** * Pros: Efficient and safe way to handle cyclic graphs, introduced in ECMAScript 2020 for better performance and security. * Cons: + Limited browser support (requires modern browsers that have implemented the `structuredClone()` function). + More complex implementation. **Library/Feature** The test uses the `structuredClone()` function, which is a feature introduced in ECMAScript 2020. This library-like function provides a more efficient and safer way to create deep copies of objects while handling cyclic graphs. **Special JS features or syntax** There are no special JavaScript features or syntax mentioned in this benchmark. The focus is on comparing the performance of two methods for serializing and deserializing an object with a cyclic graph. **Other alternatives** If the `structuredClone()` function is not supported by modern browsers, other approaches can be used: 1. Recursive copying using `JSON.parse(JSON.stringify(obj))` with a custom replacer function to handle circular references. 2. Using libraries like Lodash or JSON5 that provide more comprehensive support for handling cyclic graphs. In summary, the benchmark tests the performance of two methods for serializing and deserializing an object with a cyclic graph: `JSON.parse(JSON.stringify(obj));` and `structuredClone(obj);`. The latter is considered safer and more efficient, but its implementation requires modern browsers that have implemented this feature.
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
JSON.stringify vs structuredClone (with deeply nested objects)
Comments
Confirm delete:
Do you really want to delete benchmark?