Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce vs classic for2
(version: 1)
Comparing performance of:
Object.fromEntries vs Reduce (reuse object) vs For
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const data = Array.from(Array(10000).keys());
Tests:
Object.fromEntries
data.map((idx) => [`id-${idx}`, idx]);
Reduce (reuse object)
data.reduce((acc, idx) => { acc[`id-${idx}`] = idx; return acc; }, {});
For
const a = {} for (let idx of data) { a[`id-${idx}`] = idx; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce (reuse object)
For
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.fromEntries
8035.0 Ops/sec
Reduce (reuse object)
1532.7 Ops/sec
For
1642.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark compares three different methods of transforming an array of indices into an object using distinct JavaScript techniques. The methods tested are `Object.fromEntries`, `Array.prototype.reduce`, and a classic `for...of` loop. The transformation involves mapping each index into a string key of the format `id-{index}` with the index itself as the value. ### Methods Compared 1. **Object.fromEntries** - **Implementation**: `data.map((idx) => [`id-${idx}`, idx]);` - **Overview**: This approach utilizes `Array.prototype.map` to create an array of key-value pairs, which is then converted into an object using `Object.fromEntries`. - **Pros**: - Readable and concise code. - Utilizes built-in JavaScript methods that express the intent clearly. - **Cons**: - Requires two passes: one to build the array of entries and another to convert it back to an object, which could incur additional overhead. 2. **Reduce (reuse object)** - **Implementation**: `data.reduce((acc, idx) => { acc[`id-${idx}`] = idx; return acc; }, {});` - **Overview**: The `reduce` method is used to accumulate results into an object directly by mutating the accumulator. - **Pros**: - Efficient as it transforms the array into an object in a single pass. - Can lead to better performance if the size of the input grows. - **Cons**: - Slightly less understandable at a glance compared to `Object.fromEntries`, which may affect maintainability, especially for less experienced developers. 3. **Classic For Loop** - **Implementation**: `const a = {}; for (let idx of data) { a[`id-${idx}`] = idx; }` - **Overview**: A straightforward iteration through the array using a `for...of` loop, directly assigning keys and values to an object. - **Pros**: - Potentially the best performance as it involves minimal overhead (just one iteration). - Highly readable and easy to understand for developers familiar with traditional control structures. - **Cons**: - Slightly more verbose than the `reduce` and `Object.fromEntries` methods, which may lead to a potential increase in code length. ### Benchmark Results The benchmark results show how many executions per second each method executed. The results are: - **Object.fromEntries**: 7404.38 executions/second - **For loop**: 877.18 executions/second - **Reduce (reuse object)**: 835.78 executions/second From these results, it's clear that `Object.fromEntries` significantly outperforms both the `for...of` loop and `reduce` in this specific scenario. This performance edge can likely be attributed to its optimal implementation within the JavaScript engine. ### Other Considerations - **Readability vs. Performance**: While `Object.fromEntries` is more performant in this benchmark, choosing between these methods can heavily depend on code maintainability and clarity. For cleaner code, the concise nature of `Object.fromEntries` or `reduce` may be beneficial if performance is not a critical concern. - **Alternative Methods**: Other alternatives for constructing objects from arrays include using `Array.prototype.forEach` for side-effect mutations or leveraging `Map` and its transformations. However, these alternatives generally do not yield significant performance improvements for typical use cases compared to the methods tested. In summary, each method has its trade-offs regarding performance, readability, and complexity. The optimal choice can vary depending on specific application needs, code standards, and the expected size and structure of the input data.
Related benchmarks:
Reduce vs Map+FromEntries vs for loop
Convert Array to Object - Object.fromEntries vs reduce
Object.fromEntries vs reduce v21
Object.fromEntries vs reduce vs for loop
Convert Array to Object 2 - Object.fromEntries vs reduce vs forEach
Object.fromEntries vs reduce test 2
Reduce (object reuse) vs For loop
Object.fromEntries vs reduce vs Map vs for of vs forEach
Object.fromEntries vs reduce vs classic for v3
Comments
Confirm delete:
Do you really want to delete benchmark?