Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
structuredClone vs spread vs Object.assign()
(version: 0)
Comparing performance of:
structuredClone() vs spread syntax vs Object.assign()
Created:
3 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 }
Tests:
structuredClone()
const newObj = structuredClone(oldObj)
spread syntax
const newObj = {...oldObj}
Object.assign()
const newObj = Object.assign({}, oldObj)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
structuredClone()
spread syntax
Object.assign()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
15 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Safari/537.36
Browser/OS:
Chrome 148 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
structuredClone()
950370.4 Ops/sec
spread syntax
51578236.0 Ops/sec
Object.assign()
8132698.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is being tested in the provided JSON. The benchmark compares three methods for creating a shallow copy of an object: 1. `structuredClone()`: This method was introduced in ECMAScript 2020, and it creates a new object that contains all own property keys and values from the original object. 2. Spread syntax (`{...oldObj}`): This method uses the spread operator to create a new object with the same own properties as the original object. 3. `Object.assign()` (with `{}` as the second argument): This method creates a shallow copy of an object by copying all its own enumerable own properties to a new object. **Options being compared:** * `structuredClone()`: A relatively new method introduced in ECMAScript 2020, which creates a new object with all own property keys and values from the original object. * Spread syntax (`{...oldObj}`): An older method that uses the spread operator to create a new object with the same own properties as the original object. * `Object.assign()`: A widely supported method for creating shallow copies of objects. **Pros and Cons:** 1. `structuredClone()`: * Pros: Efficient, modern approach, preserves all own property keys and values. * Cons: Only available in ECMAScript 2020+ browsers and engines. 2. Spread syntax (`{...oldObj}`): * Pros: Simple and widely supported, creates a new object with the same properties as the original. * Cons: Can be slower than `structuredClone()` for large objects. 3. `Object.assign()`: * Pros: Widely supported, efficient for small to medium-sized objects. * Cons: Creates a shallow copy of only enumerable own properties. **Library and purpose:** None mentioned in the provided benchmark definition JSON. **Special JS feature or syntax:** 1. `structuredClone()` uses the ECMAScript 2020+ standard, which is not widely supported yet. **Other alternatives:** If none of these methods are available, you can also use: * `JSON.parse(JSON.stringify(oldObj))`: This method creates a shallow copy of an object by serializing it to a JSON string and then parsing the string back into an object. * Manual looping over properties using `Object.keys()`, `forEach()`, and assignment. Note that these alternatives have different performance characteristics and may not preserve all own property keys and values.
Related benchmarks:
Spread vs Object.assign (modify ) vs Object.assign (new)
JavaScript spread operator vs Object.assign vs new object performance
object spread vs Object.assign
JavaScript spread operator vs Object.assign performance fixed 2
JavaScript spread operator vs Object.assign performance test number 99
Comments
Confirm delete:
Do you really want to delete benchmark?