Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON.stringify vs structuredClone 2dsfasr12qe12321312331312321
(version: 1)
JSON.stringify vs structuredClone 2dsfasr12qe12321312331312321
Comparing performance of:
JSON.stringify vs structuredClone
Created:
one year 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.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:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36 Edg/135.0.0.0
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON.stringify
2168862.5 Ops/sec
structuredClone
440605.2 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark evaluates the performance of two JavaScript methods for creating deep copies of objects: `JSON.stringify` and `structuredClone`. Each approach serves the purpose of cloning an object, but they differ significantly in how they achieve this, the types of objects they can handle, and their performance. ### Comparison of Options 1. **JSON.stringify**: - **Description**: This method converts a JavaScript value (typically an object) into a JSON string. It is often followed by `JSON.parse` to reconstruct the original object. - **Pros**: - Commonly understood and widely used. - Compatible with any object that can be serialized to JSON (e.g., simple objects, arrays). - **Cons**: - Can only handle objects that are serializable to JSON. This means it fails for complex data types like functions, Dates, `undefined`, and cyclic references. - The process involves two steps (stringifying and then parsing), which could incur performance overhead, especially for large objects. 2. **structuredClone**: - **Description**: This method creates a deep clone of a given object, handling many more types than JSON serialization, such as Dates, Map, Set, ArrayBuffer, and more. - **Pros**: - More versatile as it can handle a wider range of object types, including those with circular references. - Typically faster for deep copying complex objects since it does not require converting to a string and then parsing back. - **Cons**: - Less universally supported in older environments or older JavaScript engines compared to `JSON.stringify`. As of recent updates, `structuredClone` is generally available in modern browsers but may not be available in older environments. ### Benchmark Results The benchmark results show the number of executions per second for each method: - **JSON.stringify executed approximately 3,020,609.75 times per second.** - **structuredClone executed approximately 706,589.94 times per second.** ### Performance Considerations While `JSON.stringify` demonstrates higher execution rates in this benchmark, the choice between these methods should consider: - The complexity of the objects you wish to clone. - The need for compatibility with various data types. If you're only dealing with simple objects, `JSON.stringify` may be sufficient. However, for more complex cases, `structuredClone` is the more robust option despite its slower performance in this specific benchmark. ### Alternative Approaches Other alternatives for deep copying objects in JavaScript include: - **Lodash's `cloneDeep` Method**: A library method that provides a deep copy functionality with full support for various data types. It is well-optimized and can handle cases that `JSON.stringify` and even `structuredClone` struggle with. - **Manual Copying**: In some scenarios, manually copying properties may be an option, though this is generally not optimal for complex objects. In summary, both `JSON.stringify` and `structuredClone` have their strengths and weaknesses regarding deep copying, and the choice should be guided by the requirements of the specific use case.
Related benchmarks:
JSON.stringify vs structuredClone
Test of JSON Stringify vs. structuredClone
JSON.stringify vs structuredClone 3
json.parse(stringify()) vs structuredClone
JSON.stringify vs structuredClone, modifications
JSON.stringify vs structuredClone vs _.cloneDeep
structuredClone(JSON.stringify) vs structuredClone
structuredClone(JSON.stringify) vs structuredClone 2
JSON.stringify vs structuredClone 2dsfasr12qe12321312331312321 2312312
Comments
Confirm delete:
Do you really want to delete benchmark?