Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
json.parse(stringify()) vs structuredClone
(version: 0)
Comparing performance of:
Json Clone 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...' } }; var myCopy = null;
Tests:
Json Clone
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 Clone
structuredClone
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON benchmark definition and test cases. **Benchmark Definition:** The benchmark measures the performance of two approaches for creating a deep copy of an object: 1. `JSON.parse(JSON.stringify(MyObject))` 2. `structuredClone(MyObject)` **What is being tested?** The benchmark tests the execution time of these two approaches on a specific JavaScript object (`MyObject`). The object contains nested properties, including another object with a string that describes its purpose. **Options compared:** Two options are compared: 1. **`JSON.parse(JSON.stringify(MyObject))`**: This approach uses the `JSON.parse()` method to parse a JSON representation of the object and then creates a new object from it. 2. **`structuredClone(MyObject)`**: This is a newer approach introduced in ECMAScript 2020, which creates a deep clone of an object without serializing it to a string. **Pros and Cons:** 1. **`JSON.parse(JSON.stringify(MyObject))`**: * Pros: + Widely supported by older browsers. + Easy to implement. * Cons: + Can be slower than `structuredClone()` due to the extra parsing step. + May not work correctly with certain data types (e.g., symbols, sets). 2. **`structuredClone(MyObject)`**: * Pros: + Faster execution time compared to `JSON.parse(JSON.stringify(MyObject))`. + More efficient for large objects. * Cons: + Requires modern browsers that support ECMAScript 2020 (Chrome 117 and above). + Less widely supported by older browsers. **Library:** The benchmark uses the `structuredClone` function, which is a built-in JavaScript function introduced in ECMAScript 2020. Its purpose is to create a deep clone of an object without serializing it to a string. **Special JS feature or syntax:** None mentioned in the provided JSON. **Other alternatives:** If you need to support older browsers that don't have `structuredClone()` support, you can consider using: 1. **`JSON.parse(JSON.stringify(MyObject))`:** * A widely supported approach, but slower than `structuredClone()`. 2. **Manual deep cloning:** * Creating a new object and recursively copying its properties. * More verbose and error-prone, but provides more control over the cloning process. In summary, `structuredClone()` is a newer, faster approach for creating a deep clone of an object, while `JSON.parse(JSON.stringify(MyObject))` is a widely supported, albeit slower alternative. The choice between these approaches depends on your specific requirements, browser support, and performance considerations.
Related benchmarks:
Lodash cloneDeep vs structuredClone deep array
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)
Comments
Confirm delete:
Do you really want to delete benchmark?