Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JavaScript reduce vs Object.assign performance v2
(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 arrayOfObjects = [{ moreData: 'foo bar' }, { evenMore: 'data' }] const finalObject = arrayOfObjects.reduce((result, object) => ({ ...result, ...object }), { '...': true })
Using Object.assign
const arrayOfObjects = [{ moreData: 'foo bar' }, { evenMore: 'data' }] const finalObject = Object.assign({ '...': true }, ...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:
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 break down the JavaScript microbenchmark on MeasureThat.net. **Benchmark Description** The benchmark tests the performance of two different approaches to merge objects in JavaScript: 1. Using `Array.prototype.reduce()` 2. Using `Object.assign()` (specifically, with `...` spread operator) **Options Compared** The two options being compared are: * **Using `Array.prototype.reduce()`**: This method iterates over each object in the array and merges its properties into a new object using a callback function. * **Using `Object.assign()` with spread operator (`...`)**: This method creates a new object by copying all enumerable own properties from multiple source objects (in this case, an array of objects) and merging their properties. **Pros and Cons** * **Using `Array.prototype.reduce()`**: + Pros: Can handle nested objects and arrays, provides more control over the merge process. + Cons: Can be slower than `Object.assign()` for simple merges, has a higher memory overhead due to the creation of an intermediate accumulator object. * **Using `Object.assign()` with spread operator (`...`)**: + Pros: Faster and more efficient than `reduce()`, handles nested objects and arrays more seamlessly, and is generally more concise. + Cons: Can be less flexible than `reduce()`, as it only merges enumerable own properties. **Library/Functionality Used** In this benchmark, the following libraries/functions are used: * None explicitly mentioned. However, `Object.prototype.hasOwnProperty()` is implicitly used in the reduce callback function. **Special JS Feature/Syntax** The benchmark uses the spread operator (`...`) which was introduced in ECMAScript 2015 (ES6). This feature allows for more concise and readable code when merging objects or arrays. **Alternative Approaches** Other approaches to merge objects in JavaScript include: * Using `for...in` loops with `Object.defineProperty()` and `Object.assign()` * Using a recursive function * Using a library like Lodash's `merge()` function These alternatives may offer different trade-offs in terms of performance, readability, and maintainability. However, for most use cases, the spread operator (`...`) and `Object.assign()` remain popular choices due to their simplicity and efficiency. Note that the benchmark does not test other aspects of JavaScript performance, such as memory allocation, garbage collection, or parallel execution.
Related benchmarks:
JavaScript reduce vs Object.assign performance
Object.keys(object).includes(key) vs key in object
Object.fromEntries on array vs reduce on array
Reduce vs Assign
Comments
Confirm delete:
Do you really want to delete benchmark?