Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
structuredClone vs spread vs Object.assign() vs JSON.parse(JSON.stringify)
(version: 0)
Comparing performance of:
structuredClone() vs spread syntax vs Object.assign() vs JSON
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var oldObj = { "single": false, "applyOverTaxes": false, "discountType": "1", "value": 100, "minimumAmount": 0, "quantity": 1, "suffixLength": 8, "code": "IF5R0AFX", "vertical": true, "allowedPaymentMethods": null, "allowedBanks": null, "types": undefined, "partners": "Many", "startDate": "2020-10-21T00:00:00.000Z", "endDate": "2020-10-21T00:00:00.000Z", "applyed": false, "myarr": [{a: 1, b: "2", c: [3,4,5]}] }
Tests:
structuredClone()
const newObj = structuredClone(oldObj)
spread syntax
const newObj = {...oldObj}
Object.assign()
const newObj = Object.assign({}, oldObj)
JSON
const newObj = JSON.parse(JSON.stringify(oldObj))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
structuredClone()
spread syntax
Object.assign()
JSON
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):
**Benchmark Explanation** The provided benchmark measures the performance of four different approaches for cloning or serializing JavaScript objects: 1. **structuredClone()**: This is a built-in JavaScript function introduced in ECMAScript 2022, which creates a shallow clone of an object, including its enumerable properties and their values. 2. **Spread syntax (Object spread)**: This approach uses the spread operator (`...`) to create a new object with the same properties as the original object. 3. **Object.assign()**: This method copies the properties from one or more source objects to a target object. 4. **JSON.parse(JSON.stringify())**: This approach serializes the object using JSON.stringify() and then parses the resulting string back into an object. **Options Compared** Each option is compared in terms of: * Execution time (ExecutionsPerSecond) * Number of executions per second **Pros and Cons** * **structuredClone()**: + Pros: Efficient, easy to use, creates a shallow clone that preserves the original object's properties. + Cons: Only available in ECMAScript 2022 and later versions; may not work with older browsers or environments. * **Spread syntax (Object spread)**: + Pros: Easy to use, efficient, and compatible with most modern browsers and environments. + Cons: May not work with certain types of objects (e.g., Date, RegExp) that are serialized using JSON.stringify(). * **Object.assign()**: + Pros: Widely supported across different browsers and environments; easy to understand. + Cons: Can be slower than the other options, especially for large objects. * **JSON.parse(JSON.stringify())**: + Pros: Easy to use and widely supported across different browsers and environments. + Cons: May not work with certain types of objects (e.g., Date, RegExp) that are serialized using JSON.stringify(); can be slower than the other options. **Library Usage** In this benchmark, none of the libraries are explicitly mentioned. However, it is likely that the benchmark uses a library like `structuredClone` polyfill for older browsers or environments that do not support the `structuredClone()` function natively. **Special JS Feature or Syntax** The benchmark does not use any special JavaScript features or syntax other than those mentioned above.
Related benchmarks:
JavaScript spread operator vs Object.assign performance (Fair case)
JavaScript spread operator vs Object.assign performance - non-destructive merge
JavaScript spread operator vs Object.assign performance with 3 objects to merge fork
JavaScript spread operator vs Object.assign performance fixed 2
JS spread operator vs Object.assign performance
Comments
Confirm delete:
Do you really want to delete benchmark?