Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON.stringify&parse vs structuredClone
(version: 0)
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
Comparing performance of:
JSON.stringify&parse vs Native structuredClone
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
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&parse
myCopy = JSON.parse(JSON.stringify(MyObject));
Native 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&parse
Native structuredClone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 17_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/120.0.6099.119 Mobile/15E148 Safari/604.1
Browser/OS:
Chrome Mobile iOS 120 on iOS 17.2
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON.stringify&parse
1543453.0 Ops/sec
Native structuredClone
876396.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmarking test case on MeasureThat.net. The benchmark compares the performance of two approaches for cloning an object: `JSON.stringify()` and `parse()` combination versus the native `structuredClone()` function. **What is being tested?** Two individual test cases are compared: 1. **JSON.stringify&parse**: This approach involves converting the original object to a JSON string using `JSON.stringify()`, parsing the resulting string back into an object using `JSON.parse()`, and then assigning the result to a new variable (`myCopy`). 2. **Native structuredClone**: This approach uses the native `structuredClone()` function, which is available in modern browsers, to create a deep copy of the original object. **Options compared** The benchmark compares the performance of these two approaches on an object with nested properties (`MyObject`). The object contains: * An integer property (`myNumber`) * A boolean property (`myBoolean`) * Another object with string properties (`jayson`) **Pros and Cons of each approach:** 1. **JSON.stringify&parse**: * Pros: + Widely supported in older browsers (IE 11+, Firefox 4+, Safari 5.1+, Chrome 10+) * Cons: + Can be slower due to the overhead of string conversion and parsing + May not preserve certain types of data (e.g., Date, RegExp) correctly 2. **Native structuredClone**: * Pros: + Generally faster than `JSON.stringify&parse` + More reliable for preserving complex data structures and types * Cons: + Requires modern browsers that support the `structuredClone()` function (Chrome 120+, Firefox 88+, Safari 15.2+) **Library and its purpose** The benchmark uses the Lodash library, which is a utility library for functional programming in JavaScript. In this case, it provides the `structuredClone()` function. **Special JS feature or syntax** No special features or syntax are mentioned in the benchmark. However, note that modern browsers support advanced features like structured cloning, which can be useful for performance-critical code. **Other alternatives** If you need to clone objects without using `JSON.stringify&parse` or `structuredClone()`, other alternatives include: * Using a library like Lodash's `cloneDeep()` function * Implementing your own deep copying function using recursion and/or iteration * Utilizing browser-specific APIs, such as the WebAssembly binary format for cloning objects Keep in mind that each approach has its trade-offs and may have different performance characteristics depending on the specific use case.
Related benchmarks:
Lodash cloneDeep vs structuredClone vs JSON.parse/stringify
Lodash cloneDeep vs structuredClone vs JSON Stringify/Parse
Lodash cloneDeep vs structuredClone vs JSON parse/stringify
Lodash cloneDeep vs structuredClone vs Json.stringify
lodash clonedeep vs json.parse(stringify()) vs structuredClone
Comments
Confirm delete:
Do you really want to delete benchmark?