Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Creating dictionary with fromEntries vs reduce
(version: 0)
Comparing performance of:
fromEntries vs reduce
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = [ ...Array(10000) ].map(() => ({ id: Math.round(Math.random() * 1e8), value: `Information ${Math.round(Math.random() * 1e5)}` }))
Tests:
fromEntries
Object.fromEntries(data.map((obj => [obj.id, obj])))
reduce
data.reduce((acc, obj) => { acc[obj.id] = obj return acc }, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
fromEntries
reduce
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/127.0.0.0 Safari/537.36
Browser/OS:
Chrome 127 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
fromEntries
1280.4 Ops/sec
reduce
1513.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **What is being tested?** The provided benchmark tests two approaches for creating an object from an array of key-value pairs: `Object.fromEntries` and `Array.prototype.reduce()`. The test data consists of a large array (`data`) with 10,000 objects, each containing an `id` and a `value`. **Options being compared** The two options being tested are: 1. **`Object.fromEntries(data.map((obj => [obj.id, obj])))`**: This method creates a new object by iterating over the input array and mapping each element to a key-value pair using the spread operator (`[...]`). The resulting object is then passed to `Object.fromEntries`, which converts it back into an object. 2. **`data.reduce((acc, obj) => { acc[obj.id] = obj; return acc; }, {})`**: This method uses the `reduce()` method to iterate over the input array and accumulate a new object. For each element, it sets the property on the accumulator object (`acc`) using the `id` as the key. **Pros and Cons of each approach** 1. **`Object.fromEntries`**: * Pros: + More concise and readable code. + Less prone to errors due to the explicit structure of the input data. * Cons: + May be slower due to the additional overhead of creating an intermediate array and then converting it back into an object. 2. **`Array.prototype.reduce()`**: * Pros: + More flexible and can handle complex accumulation logic. * Cons: + Less readable code, as it requires more mental effort to understand the accumulator's state. + May be slower due to the additional overhead of managing the accumulator object. **Library usage** Neither approach uses a library specifically. However, `Object.fromEntries` is a relatively new method introduced in ECMAScript 2019 (ES2020), which provides a concise way to create an object from key-value pairs. **Special JS feature or syntax** There are no special JavaScript features or syntax used in this benchmark. **Other alternatives** If you wanted to write a similar benchmark, here's an alternative approach using `Array.prototype.forEach()`: ```javascript const result = {}; data.forEach((obj) => { result[obj.id] = obj; }); ``` This approach is often considered less readable and more error-prone than the original `reduce()` method. Another alternative could be using a library like Lodash, which provides a `fromEntries` function: ```javascript const _ = require('lodash'); const result = _.fromEntries(data.map((obj) => [obj.id, obj])); ``` Keep in mind that this approach would introduce additional overhead due to the use of an external library. In summary, the benchmark on MeasureThat.net tests two approaches for creating an object from an array of key-value pairs: `Object.fromEntries` and `Array.prototype.reduce()`. While both approaches have their pros and cons, `Object.fromEntries` provides a concise and readable solution with less overhead, while `reduce()` offers more flexibility but at the cost of readability.
Related benchmarks:
Object.fromEntries vs create temp object vs Array.reduce
Object.fromEntries on array vs reduce on array
flatMap vs reduce test
Object.fromEntries vs reduce round 2
push vs spread (reduce array)
Comments
Confirm delete:
Do you really want to delete benchmark?