Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce object vs object.fromentries (1K records)
(version: 0)
Comparing performance of:
reduce vs Object.fromEntries
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const array = Array.from({ length: 1000 }, (_, i) => ({ key: `key${i}`, value: i }));
Tests:
reduce
function reduce() { return array.reduce((acc, { key, value }) => { acc[key] = value; return acc; }, {}); }
Object.fromEntries
function objectFromEntries() { return Object.fromEntries(array.map(({ key, value }) => [key, value])); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
Object.fromEntries
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0
Browser/OS:
Chrome 127 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
171484560.0 Ops/sec
Object.fromEntries
194263264.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is defined by two JavaScript functions: `reduce` and `objectFromEntries`. Both functions are designed to process an array of objects with key-value pairs. The main difference between them lies in how they create and manipulate the resulting object. **Reduce Function** The `reduce` function takes an initial value (`{}`) and iterates through the array, accumulating values into the accumulator (`acc`). In this implementation, each object in the array is assigned to a property of the accumulator, effectively creating a new object with the key-value pairs. The resulting object is then returned. **Object.fromEntries Function** The `objectFromEntries` function uses the `Object.fromEntries()` method, which creates a new object from an iterable (in this case, an array of key-value pairs). This approach is more concise and modern than the `reduce` function. **Comparison** The benchmark compares the performance of these two approaches: * **Reduce Function**: This implementation has a higher memory overhead due to the need to create an accumulator object for each iteration. It also involves more explicit operations, which can be slower. * **Object.fromEntries Function**: This approach is often faster and more efficient because it leverages the optimized `Object.fromEntries()` method. **Pros and Cons** **Reduce Function:** Pros: * Easy to understand for developers familiar with array iteration * Can be used in scenarios where the resulting object needs to be iterated over further Cons: * Higher memory overhead due to accumulator object creation * Less concise than the `objectFromEntries` function **Object.fromEntries Function:** Pros: * Faster and more efficient, thanks to optimized implementation * More concise and readable code Cons: * May be less familiar or intuitive for developers without experience with modern JavaScript features * Not all browsers support `Object.fromEntries()` (though it's widely supported) **Library Usage** In this benchmark, there is no explicit library usage. However, the `Array.from()` method used in both implementations relies on the built-in `Array.prototype.from()` method, which is part of the JavaScript standard library. **Special JS Features/Syntax** There are no specific JavaScript features or syntax mentioned in this benchmark that would require a deeper understanding of advanced topics like closures, async/await, or modern JavaScript paradigms. The code focuses on basic array manipulation and object creation using modern methods. **Alternatives** If you're looking for alternatives to these two approaches, consider the following: * **For loops**: You can use traditional `for` loops with index iteration to create an object from an array of key-value pairs. * **Spread operators**: Another approach is to use the spread operator (`{...}`) to create a new object and then iterate through the array, assigning values to properties. * **Array.prototype.map()** + **Object.assign()**: You can also use `array.map()` to transform an array into an array of key-value pairs and then pass it to `Object.assign()` to create a new object. Keep in mind that each approach has its trade-offs in terms of performance, readability, and maintainability.
Related benchmarks:
Object.fromEntries vs create temp object vs Array.reduce
Object.fromEntries on array vs reduce on array
Object.fromEntries vs reduce v21
Object.fromEntries vs reduce round 2
Object.fromEntries(Array.map) vs Array.reduce (with different methods)
Comments
Confirm delete:
Do you really want to delete benchmark?