Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs {...newValue} 2
(version: 1)
Comparing performance of:
With Object.assign vs With {...newValue}
Created:
5 years ago
by:
Registered User
Jump to the latest result
Tests:
With Object.assign
const row = {id: 1, label: 'asdf'}; const newRow = Object.assign({}, row, { label: `${row.label} !!!` })
With {...newValue}
const row = {id: 1, label: 'asdf'}; const newRow = {...row, label: `${row.label} !!!` }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
With Object.assign
With {...newValue}
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
gemma2:9b
, generated one year ago):
This benchmark on MeasureThat.net compares two ways of creating a new object based on an existing one in JavaScript: **1. `Object.assign({}, row, { label: `${row.label} !!!` })`:** This approach uses the `Object.assign()` method to create a new object by merging properties from three sources: - An empty object (`{}`) that acts as the initial target for the merged properties. - The existing `row` object, whose properties will be copied. - A new object `{ label: `${row.label} !!!` }`, which adds a modified `label` property to the new object. **2. `{...row, label: `${row.label} !!!`}`:** This approach uses the spread syntax (`...`), introduced in ES6 (ECMAScript 2015). It creates a new object by spreading the properties of the existing `row` object and adding a modified `label` property directly. **Pros and Cons:** * **`Object.assign()`:** * **Pros:** More versatile, as it can merge objects beyond simple key-value pairs. * **Cons:** Can be more verbose than the spread syntax. May perform slightly worse in some scenarios due to extra method calls. * **Spread Syntax (`{...row,...}`):** * **Pros:** Concise and readable, especially for simpler object merges. Often performs better than `Object.assign()` in benchmarks. * **Cons:** Less flexible for more complex merging scenarios. **Considerations:** * **Readability:** The spread syntax (`{...}`) is generally considered more concise and readable. * **Performance:** In this specific benchmark, the spread syntax outperformed `Object.assign()`. However, performance can vary depending on the complexity of the objects being merged and other factors. **Alternatives:** While not directly compared in this benchmark, other alternatives for object manipulation include: * **Object Literal Notation:** Manually creating new objects by listing key-value pairs (e.g., `{ id: 1, label: 'asdf' }`). * **Lodash/Underscore Libraries:** These libraries provide functions like `_.assign()` and `_.merge()` that offer more advanced object merging capabilities. Let me know if you have any other questions!
Related benchmarks:
Spread vs Object.assign (modify ) vs Object.assign (new)
Object.assign vs direct copy
JS Object assign vs object new property 2
Object.assign() vs spread operator (New object)
Object.assign vs mutation
Comments
Confirm delete:
Do you really want to delete benchmark?