Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Convert Array to Object 2 - Object.fromEntries vs reduce vs forEach
(version: 0)
Comparing performance of:
Object.fromEntries vs Reduce (reuse object) vs Reduce (creating temporary objects) vs for of vs for each
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = Array.from(Array(10000).keys()).map(i => ({id: i, value: `val_${i}`}));
Tests:
Object.fromEntries
Object.fromEntries(data.map(obj => [obj.id, obj]));
Reduce (reuse object)
data.reduce((acc, obj) => { acc[obj.id] = obj; return acc; }, {});
Reduce (creating temporary objects)
data.reduce((acc, obj) => ({ ...acc, [obj.id]: obj }), {});
for of
const acc = {} for (const obj of data) { acc[obj.id] = obj }
for each
const acc = {} data.forEach((obj) => { acc[obj.id] = obj })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce (reuse object)
Reduce (creating temporary objects)
for of
for each
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 what is tested on the provided JSON that represents the benchmark. **Benchmark Description** The benchmark compares three different approaches to convert an array of objects into an object: 1. `Object.fromEntries`: This method creates an object from an array of key-value pairs using the `fromEntries` function. 2. `Reduce (reuse object)`: This approach uses the `reduce` function to iterate over the array and accumulate the values into a single object. The accumulator object is reused throughout the iteration. 3. `for each` and `for of`: These two approaches use loops to iterate over the array and build an object. **Options Compared** * `Object.fromEntries` vs `Reduce (reuse object)`: This comparison tests how the two methods perform in terms of execution speed, memory usage, and readability. * `Reduce (reuse object)` vs `for each`: This comparison tests whether reusing the accumulator object in a single `reduce` call is faster than using separate loops for each iteration. * `Object.fromEntries` vs `for each` and `for of`: This comparison tests how the three methods compare in terms of execution speed. **Pros and Cons** Here are some pros and cons for each approach: * **`Object.fromEntries`**: + Pros: Readable, concise, and easy to understand. + Cons: May have performance issues due to the overhead of creating a new object for each iteration. * **`Reduce (reuse object)`**: + Pros: Can be faster than other methods since it reuses the accumulator object. + Cons: Requires careful handling of initial values, and may lead to unexpected behavior if not implemented correctly. * **`for each` and `for of`**: + Pros: Simple, easy to understand, and can be fast for small arrays. + Cons: May have performance issues due to the overhead of creating a new object on each iteration. **Library and Special JS Features** None of the benchmark test cases use any external libraries or special JavaScript features beyond the standard `Array` and `Object` APIs. **Other Alternatives** There are several other approaches that could be used to convert an array of objects into an object, such as: * Using `Map` instead of `Object`: `const map = new Map(data.map(obj => [obj.id, obj]));` * Using `Array.prototype.reduce()` with a different accumulator type (e.g., `Array`) or handling initial values differently. * Using `Array.prototype.forEach()` or other loop constructs to iterate over the array and build an object. Overall, the benchmark provides valuable insights into the performance characteristics of different JavaScript methods for converting arrays of objects into objects.
Related benchmarks:
Object.fromEntries vs create temp object vs Array.reduce
Object.fromEntries vs reduce - convert Array to Object
Convert Array to Object - Object.fromEntries vs reduce
Object.fromEntries on array vs reduce on array
Comments
Confirm delete:
Do you really want to delete benchmark?