Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
structuredClone vs spread object vs json
(version: 0)
Comparing performance of:
Lodash cloneDeep vs Spread operator vs Json stringify
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var voucher = { "single": false, "applyOverTaxes": false, "discountType": "1", "value": 100, "minimumAmount": 0, "quantity": 1, "suffixLength": 8, "code": "IF5R0AFX", "vertical": [], "allowedPaymentMethods": [], "allowedBanks": [], "types": [], "partners": [], "startDate": "2020-10-21T00:00:00.000Z", "endDate": "2020-10-21T00:00:00.000Z", "applyed": false }
Tests:
Lodash cloneDeep
const copy = window.structuredClone(voucher);
Spread operator
const copy = { ...voucher };
Json stringify
const copy = JSON.parse(JSON.stringify(voucher));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash cloneDeep
Spread operator
Json stringify
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 benchmark and its options. **Benchmark Definition** The benchmark is designed to compare three methods for cloning or copying a JavaScript object: `structuredClone`, the spread operator (`{ ... }`), and `JSON.stringify()` followed by `JSON.parse()`. The goal is to measure which method provides the fastest execution time for a given object. **Options Compared** 1. **Structured Clone**: This method uses the `structuredClone()` function, which was introduced in ECMAScript 2022. It creates a deep clone of an object by recursively copying all own properties and their values. * Pros: Efficient, fast, and modern. * Cons: Only available in modern browsers and JavaScript engines (e.g., V8). 2. **Spread Operator**: This method uses the spread operator (`{ ... }`) to create a shallow copy of an object by only copying its own enumerable properties. * Pros: Fast, widely supported, and easy to use. * Cons: Only creates a shallow copy, which means it may not work correctly for objects with circular references or deeply nested properties. 3. **JSON Stringify + JSON Parse**: This method uses `JSON.stringify()` to convert the object to a string, followed by `JSON.parse()` to recreate the object from the string. * Pros: Widely supported and easy to use, but can be slower than other methods due to the conversion overhead. * Cons: May not work correctly for objects with circular references or deeply nested properties. **Library** None of these options rely on external libraries. The `structuredClone()` function is a built-in method in modern JavaScript engines, while the spread operator and `JSON.stringify()`/`JSON.parse()` are part of the standard library. **Special JS Feature/Syntax** The benchmark uses modern JavaScript features, specifically: * **Structured Clone**: Introduced in ECMAScript 2022. * **Spread Operator**: Widely supported since ECMAScript 2015. **Other Alternatives** If you need to clone or copy a JavaScript object and the above options are not suitable for your use case, consider these alternatives: * **Lodash.cloneDeep()**: A popular library that provides a deep cloning function. It's widely used but adds extra overhead due to its dependency on Lodash. * **Object.assign()**: Another method for creating a shallow copy of an object. While it's fast and widely supported, it may not work correctly for objects with circular references or deeply nested properties. In conclusion, the benchmark provides a useful comparison of three methods for cloning or copying JavaScript objects, highlighting the trade-offs between speed, simplicity, and feature support.
Related benchmarks:
cloneDeep vs spread object vs json
_.cloneDeep vs structuredClone vs spread
structuredClone vs spread vs Object.assign() vs JSON.parse(JSON.stringify)
structuredClone vs spread vs Object.assign() vs JSON.parse
Comments
Confirm delete:
Do you really want to delete benchmark?