Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript reduce vs Object.assign performance
(version: 0)
Comparing performance of:
Using reduce vs Using Object.assign
Created:
4 years ago
by:
Guest
Jump to the latest result
Tests:
Using reduce
const firstObject = { '...': true } const arrayOfObjects = [{ moreData: 'foo bar' }, { evenMore: 'data' }] const finalObject = arrayOfObjects.reduce((result, object) => ({ ...result, ...object }), { '...': true })
Using Object.assign
const firstObject = { '...': true } const arrayOfObjects = [{ moreData: 'foo bar' }, { evenMore: 'data' }] const finalObject = Object.assign(firstObject, ...arrayOfObjects);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Using reduce
Using Object.assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Using reduce
16992730.0 Ops/sec
Using Object.assign
24893022.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the JavaScript benchmarking test. **What is being tested?** The provided JSON represents two individual test cases that compare the performance of `Array.prototype.reduce()` and `Object.assign()`. Both tests involve creating an initial object, adding multiple objects to it using one of these methods, and measuring their execution times. **Options compared:** 1. **Using reduce**: This method iterates over the array of objects, applying a reduction function to each object in turn, accumulating the results into a new object. 2. **Using Object.assign()**: This method creates a shallow copy of an existing object (in this case, `firstObject`) and then merges the properties of subsequent arrays (`arrayOfObjects`) into it. **Pros and Cons:** * **Using reduce**: + Pros: - More flexible, as it allows for custom reduction functions. - Can handle more complex data structures. + Cons: - May have higher overhead due to the iteration process. - Can be slower for large datasets. * **Using Object.assign()**: + Pros: - Faster and more efficient for simple object merging. - Less overhead, as it leverages existing JavaScript optimizations. + Cons: - Limited flexibility, as it only supports shallow copying and merging. - Can lead to performance issues when dealing with large datasets or deeply nested objects. **Libraries and special features:** Neither of these test cases uses any external libraries. However, if we were to consider the broader context of JavaScript development, `Object.assign()` has been a part of the language since ECMAScript 2015 (ES6). **Special syntax:** The reduction function in the `Using reduce` test case uses an arrow function (`(result, object) => ({ ...result, ...object })`). This is a modern syntax feature introduced in ECMAScript 2015 (ES6), which allows for concise and expressive functions. **Other alternatives:** If you're interested in exploring alternative approaches, consider: * Using `Array.prototype.forEach()` or `for...of` loops to iterate over the array of objects. * Implementing a custom merging function using JavaScript's built-in `Object.keys()`, `Object.values()`, and `reduce()` methods. * Utilizing libraries like Lodash (which provides a `merge()` function) or Ramda (which offers an `object.merge()` function). Keep in mind that each alternative will have its own trade-offs in terms of performance, readability, and maintainability.
Related benchmarks:
Object.assign vs mutate
Spread vs Object.assign (modify ) vs Object.assign (new)
object spread vs Object.assign
JavaScript spread operator vs Object.assign performance - Kien Nguyen
Object.assign() vs spread operator (New object)
Comments
Confirm delete:
Do you really want to delete benchmark?