Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reducer vs fromEntries
(version: 0)
Comparing performance of:
fromEntries vs reducer vs reducer with acc
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
vartest = test = Array(10000) .fill('') .map((_, i) => ({ key: 'key' + i, value: 'value' + i }));
Tests:
fromEntries
const result = Object.fromEntries(test.map(({ key, value }) => [key, value]));
reducer
const result2 = test.reduce((acc, { key, value }) => { acc[key] = value; return acc; }, {});
reducer with acc
const result3 = test.reduce((acc, { key, value }) => ({...acc, [key]: value}), {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
fromEntries
reducer
reducer with acc
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
fromEntries
816.2 Ops/sec
reducer
2097.2 Ops/sec
reducer with acc
0.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark is designed to compare the performance of two approaches for creating an object from an array of key-value pairs: `Object.fromEntries` and the `reduce` method. **Options Compared** There are three test cases: 1. **From Entries**: Uses the `Object.fromEntries` method, which creates a new object by iterating over an iterable (in this case, an array) of key-value pairs. 2. **Reducer with Acc**: Uses the `reduce` method, which applies a callback function to each element in the array, accumulating a result object. **Pros and Cons** * **Object.fromEntries** * Pros: * More concise and readable syntax * Does not require iterating over the array twice (like the reducer approach) * Cons: * May have performance overhead due to the creation of an intermediate iterable object * **Reducer with Acc** * Pros: * Can be more efficient, as it avoids creating an intermediate iterable object * Allows for more control over the accumulator object and key-value pairs iteration order * Cons: * More verbose syntax * Requires iterating over the array twice (once to initialize the accumulator, once to update its values) **Library** In this benchmark, `Object.fromEntries` is used as a library function. It's a modern JavaScript method introduced in ECMAScript 2015 (ES6) that creates an object from an iterable of key-value pairs. **Other Considerations** When choosing between these two approaches, consider the following: * Performance: If you're dealing with large datasets or performance-critical code, the reducer approach might be more efficient. * Code Readability: The `Object.fromEntries` method is generally more concise and readable, making it a better choice for simple use cases. **Alternative Approaches** Other ways to create an object from an array of key-value pairs include: * **Array.prototype.reduce()**: Similar to the reducer approach, but without the accumulator object. * **Array.prototype.forEach()**: Iterates over the array and updates a target object. Not as efficient as `Object.fromEntries` or `reduce`, though. Here's an example using `Array.prototype.reduce()`: ```javascript const result = test.reduce((acc, { key, value }) => ({ ...acc, [key]: value }), {}); ``` And here's an example using `Array.prototype.forEach()`, assuming a target object `obj`: ```javascript test.forEach(({ key, value }) => obj[key] = value); ``` Note that the latter approach might not be suitable for all use cases, especially when dealing with large datasets or performance-critical code.
Related benchmarks:
reduce (immutable) vs map + fromEntries
Map & Object.fromEntries vs reduce
Object.fromEntries on array vs reduce on array
Object.fromEntries vs reduce round 2
Comments
Confirm delete:
Do you really want to delete benchmark?