Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JSON.stringify + JSON.parse vs structuredClone AJP
(version: 0)
Comparing performance of:
JSON.stringify + JSON.parse Sep vs structuredClone Sep
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj =[] var abc = { "name": "oblique FGRE", "duration": 15, "id": "l1cdds16-2496-4116-9f14-626860ba5a4z", "taskProtocolId": "protocol://GE@local/adult_abdomen_AbdomenAdditionalSequences_20160410115453425_5/session.xml:3D Calibration BH and FB:3", "taskProtocolResource": "protocol://GE@local/adult_abdomen_AbdomenAdditionalSequences_20160410115453425_5/session.xml", "state": "READY_TO_EXECUTE", "subState": "", "taskType": "LxScanningTask", "movable": true, "operations": { "clinicalOperations": [ { "operationType": "Execution", "name": "SCAN", "allowed": true }, { "operationType": "Execution", "name": "MANUAL_PRESCAN", "allowed": true }, { "operationType": "Execution", "name": "AUTO_PRESCAN", "allowed": true }, { "name": "CONFIRM", "allowed": false, "operationType": "Prescription" }, { "operationType": "prescription", "name": "GET_PARAMETERS", "allowed": true, "applicable": null, "message": null, "url": { "serviceKey": "task-v1", "requestMethod": "GET", "relativePath": "/tasks/l1cdds16-2496-4116-9f14-626860ba5a4z/parameters", "absolutePath": null } }, { "operationType": "Prescription", "name": "PRESCRIBE", "allowed": true, "applicable": true, "message": null, "url": { "serviceKey": "task-v1", "requestMethod": "PATCH", "relativePath": "/tasks/l1cdds16-2496-4116-9f14-626860ba5a4z", "absolutePath": null } }, { "operationType": "Prescription", "name": "RENAME", "allowed": true, "applicable": true, "message": null, "url": { "serviceKey": "task-v1", "requestMethod": "PUT", "relativePath": "/tasks/l1cdds16-2496-4116-9f14-626860ba5a4z/name", "absolutePath": null } } ] }, "context": { "examUuid": "df10b24a-e044-410b-954c-217cc23051ef" } }; for(i=0; i<256; i++){ obj.push(abc) }
Tests:
JSON.stringify + JSON.parse Sep
JSON.parse(JSON.stringify(obj));
structuredClone Sep
structuredClone(obj);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
JSON.stringify + JSON.parse Sep
structuredClone Sep
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/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
JSON.stringify + JSON.parse Sep
1121.4 Ops/sec
structuredClone Sep
76710.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark measures the performance of two JavaScript methods: `JSON.parse(JSON.stringify(obj))` and `structuredClone(obj)`. The test case uses a large object `obj` that is created by pushing 256 identical copies of another object `abc` into an array. This creates a deep copy of the object. **Options Compared** The benchmark compares the performance of two options: 1. **JSON.parse(JSON.stringify(obj))**: This method is used to serialize and then deserialize the object, which effectively creates a shallow copy. However, due to JavaScript's prototype chain, this method can also create a deep copy if the object has no circular references. 2. **structuredClone(obj)**: This method is specifically designed for creating a deep copy of objects in modern browsers (Chrome 127 and above) that support it. **Pros and Cons** 1. `JSON.parse(JSON.stringify(obj))`: * Pros: Wide browser compatibility, easy to implement. * Cons: + May not work correctly if the object has circular references or complex inheritance structures. + Can be slower than `structuredClone` for large objects. 2. `structuredClone(obj)`: * Pros: Designed specifically for deep copying, works well even with circular references and complex inheritance structures. * Cons: + Limited browser compatibility (requires Chrome 127 or above). + May not work in older browsers or environments that don't support it. **Library and Syntax** The test case uses the `structuredClone` function, which is a built-in JavaScript method introduced in ECMAScript 2022. This function is specifically designed for creating deep copies of objects. **Other Considerations** When working with large objects or complex data structures, the choice between these two methods can impact performance and accuracy. If you need to create a deep copy of an object that may have circular references or complex inheritance structures, `structuredClone` is likely a better choice. However, if you're working in an older browser or environment where `structuredClone` is not supported, `JSON.parse(JSON.stringify(obj))` may be a more viable option. **Alternatives** Other alternatives for creating deep copies of objects include: * Using the `lodash.cloneDeep` function from the Lodash library. * Implementing a custom deep copying algorithm using recursion and object iteration. * Using a library like `js-yaml` or `json-stringify-safe` to serialize and deserialize objects. However, these alternatives may have additional dependencies, complexity, or performance overhead compared to the built-in `structuredClone` function.
Related benchmarks:
Lodash cloneDeep vs structuredClone vs node-clone, cloning of configs
JSON.stringify vs structuredClone test
safe includes vs parsed object
Benchmark lodash vs JSON
JSON.parse vs StructuredClone Huge Object
Comments
Confirm delete:
Do you really want to delete benchmark?