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
llama3.1:latest
, generated one year ago):
Let's dive into the benchmark. **What is being tested?** The provided JSON represents a benchmark that compares two approaches to creating and updating an object with 1000 properties: `Object.assign` vs the spread operator (`...`). The test cases are running on a user agent (Chrome 68) and measuring the execution speed of each approach. **Test Cases:** There are two individual test cases: 1. **Spread**: This test case uses the spread operator (`...`) to create and update an object with 1000 properties. The code snippet is: ```javascript var obj = {}; for (var i = 0; i < 1000; i++) { obj = { ...obj, [i.toString()]: i }; } ``` The spread operator is used here to merge the existing object `obj` with a new property `[i.toString()]` on each iteration of the loop. 2. **Object.assign**: This test case uses the `Object.assign()` method to create and update an object with 1000 properties. The code snippet is: ```javascript var obj = {}; for (var i = 0; i < 1000; i++) { obj = Object.assign(obj, { [i.toString()]: i }); } ``` `Object.assign()` is used here to copy the existing object `obj` and merge it with a new property `[i.toString()]` on each iteration of the loop. **Library/Feature Used:** None. Both test cases use standard JavaScript features only. **Pros/Cons:** * **Spread operator (`...`)**: + Pros: concise code, easy to read, and write. + Cons: can be slower than `Object.assign()` for large objects (see benchmark results). * **Object.assign()**: + Pros: generally faster than the spread operator for large objects. + Cons: slightly more verbose code. **Other Considerations:** * Both approaches have a time complexity of O(n), where n is the number of properties in the object. However, `Object.assign()` has an additional overhead due to the creation of temporary objects. * The benchmark results suggest that `Object.assign()` is faster than the spread operator for this specific use case. **Alternatives:** If you need to create and update a large object, consider using a library like Lodash or Immutable.js, which provide optimized methods for this purpose. Another option is to use a data structure like a Map, which can be more efficient than an object for large datasets. That's it! Let me know if you have any further questions.
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?