Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.assign vs spread v2
(version: 0)
Comparing performance of:
Spread vs Object.assign
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
Spread
var obj = {} for (var i = 0; i < 1000; i++) { obj = { ...obj, [i.toString()]: i } }
Object.assign
var obj = {} for (var i = 0; i < 1000; i++) { obj = Object.assign(obj, { [i.toString()]: i }) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Spread
Object.assign
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 tests two different methods of creating an object with a large number of key-value pairs in JavaScript: **Method 1: Spread Operator (`...`)** - Code Example: `obj = { ...obj, [i.toString()]: i }` - **Explanation:** This method utilizes the spread operator (`...`) to combine existing properties from `obj` with a new key-value pair `{ [i.toString()]: i }`. In essence, it creates a new object by spreading the contents of `obj` and adding the new property. **Method 2: `Object.assign()` Method** - Code Example: `obj = Object.assign(obj, { [i.toString()]: i })` - **Explanation:** This method uses the `Object.assign()` function to merge properties from a source object (`obj`) with those of a destination object (`{ [i.toString()]: i }`). It modifies `obj` in place by adding the new property. **Pros and Cons:** * **Spread Operator:** - **Pros:** Can be more concise and readable for simple cases like this one. - **Cons:** Performance can potentially be slower than `Object.assign()` for large objects, especially on older JavaScript engines. There's also a possibility of unintended copying behavior if not used carefully with nested objects. * **`Object.assign()`:** - **Pros:** Generally considered more performant than the spread operator, particularly when dealing with large objects. More predictable behavior for deep copies and handling complex object structures. - **Cons:** Can be less concise to write compared to the spread operator. **Other Considerations:** * **Context Matters:** The best choice depends on the size of your objects and the specific use case. For small, frequently updated objects, the spread operator might be sufficient. For larger or more complex objects, `Object.assign()` is often preferred for its performance benefits. * **Alternatives:** There are other libraries (like Lodash) that provide optimized object merging functions. Consider these if your project requires extremely high performance or specialized behavior. Let me know if you'd like me to elaborate on any specific aspect!
Related benchmarks:
toFixed -> Number vs Math.round
toFixed() vs Math.round().toString()
toFixed() vs String(Math.floor()
toFixed vs Math.round() with numbers222
Instanceof VS toString for date comparison when using objects
Comments
Confirm delete:
Do you really want to delete benchmark?