Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
spread vs Object.assign for reduce callback with large dataset
(version: 0)
Comparing performance of:
with spread operator vs with object assign
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
with spread operator
const range = (from, to) => { const output = [] for(var x = from; x < to; x++){ output.push(x) } return output } range(0, 1000).reduce((acc, num) => { return { ...acc, [num]: num } }, {})
with object assign
const range = (from, to) => { const output = [] for(var x = from; x < to; x++){ output.push(x) } return output } range(0, 1000).reduce((acc, num) => { return Object.assign(acc, {[num]: num}) }, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
with spread operator
with 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.2:3b
, generated one year ago):
Let's dive into the benchmark. **What is being tested?** MeasureThat.net is testing two approaches to merge an object with another object using different methods: 1. **Spread Operator (`...`)**: The first approach uses the spread operator to merge an object with a new property. Specifically, it takes an array created by `range(0, 1000)` and reduces it to create an object where each number is mapped to itself. 2. **Object Assign (`Object.assign()`)**: The second approach uses `Object.assign()` to merge two objects. Again, the input is an array created by `range(0, 1000)`, but this time it's being merged into an empty object. **Options comparison** The two approaches have different pros and cons: * **Spread Operator (`...`)**: + Pros: concise, efficient, and easy to read. + Cons: not supported in older browsers, may cause issues with certain browser versions or configurations. * **Object Assign (`Object.assign()`)**: More widely supported across browsers, but can be less readable and more verbose. **Other considerations** When using either approach, consider the following: * In both cases, the `reduce()` method is used to create a new object. However, if you need to merge multiple arrays or objects, other methods (like `Object.assign()` with an array of properties) might be more suitable. * If performance is critical, measuring the execution time can help determine which approach is better for your specific use case. **Library and special JS features** No libraries are used in these benchmarks. However, JavaScript does support some interesting syntax: * **Destructuring**: The spread operator (`...`) uses destructuring to extract properties from an object. * **Arrow functions**: Both benchmark definitions use arrow functions (`() => { ... }`), which provide a concise way to define small functions. **Alternative approaches** If you need more control or flexibility, consider these alternative approaches: 1. **Manual object merge using `for...in` loop**: Iterate over the properties of one object and add them to another. ```javascript const obj1 = { a: 1, b: 2 }; const obj2 = {}; for (const key in obj1) { obj2[key] = obj1[key]; } ``` 2. **Using `Object.fromEntries()`**: A newer method for creating objects from arrays of key-value pairs. ```javascript const arr = [['a', 1], ['b', 2]]; const obj = Object.fromEntries(arr); ``` These alternatives may offer more control, but they might also be slower or less concise than the original approaches.
Related benchmarks:
object assign vs object spread on growing objects
JavaScript spread operator vs Object.assign performance without mutating original object
JavaScript spread operator vs Object.assign performance (same behaviour)
Object.assign() vs spread operator (New object)
JavaScript spread operator vs Object.assign performance without overwriting original object
Comments
Confirm delete:
Do you really want to delete benchmark?