Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON.stringify vs structuredClone1
(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: undefined, 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)); console.log(myCopy)
structuredClone
myCopy = structuredClone(MyObject); console.log(myCopy)
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:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON.stringify
174648.7 Ops/sec
structuredClone
136403.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and test cases to explain what's being tested, the options compared, their pros and cons, and other considerations. **Benchmark Definition:** The JSON object represents two benchmark definitions: 1. `JSON.stringify vs structuredClone1` 2. The first benchmark definition contains a script preparation code that creates an object `MyObject` with various properties, including a nested object `jayson`. It also initializes a variable `myCopy` to `null`. 3. The second benchmark definition is similar to the first one but uses `structuredClone(MyObject)` instead of `JSON.parse(JSON.stringify(MyObject))`. **Options Compared:** Two options are being compared: 1. **JSON.stringify()**: A built-in JavaScript method that converts a JavaScript value to a JSON string. 2. **structuredClone()**: A newer JavaScript method introduced in ECMAScript 2020, designed for deep copying objects while preserving their structure and relationships. **Pros and Cons:** 1. **JSON.stringify():** * Pros: + Wide browser support (since 2009) + Easy to implement * Cons: + Creates a shallow copy by default (not suitable for nested objects with cyclic references) + Can be slower than `structuredClone()` due to the overhead of serializing and parsing JSON strings 2. **structuredClone():** * Pros: + Designed specifically for deep copying, handling nested objects with cyclic references + Generally faster than `JSON.stringify()` * Cons: + Requires modern browsers that support ECMAScript 2020 ( introduced in Chrome 90 and later) + May not be supported by older browsers **Library Usage:** There is no explicit library mentioned in the benchmark definition. However, it's worth noting that `structuredClone()` is a part of the JavaScript standard library since ECMAScript 2020. **Special JS Features or Syntax:** None are explicitly used in this benchmark. The tests rely on built-in JavaScript methods and variables. **Other Considerations:** * **Memory usage:** Both options can lead to memory issues if the original object has a large number of properties or references. `structuredClone()` might be more efficient in terms of memory allocation, but it's still subject to browser limitations. * **Browser quirks:** Different browsers may exhibit varying performance differences between these two methods. **Alternatives:** If you're interested in exploring alternative approaches for deep copying JavaScript objects, consider: 1. Using a dedicated library like Lodash or Underscore.js, which provide functions for recursive object cloning (e.g., `lodash.clone()`). 2. Implementing your own recursive function to copy nested objects. 3. Utilizing other libraries or frameworks that offer built-in support for deep copying, such as React's `useCallback` and `useMemo`.
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?