Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reduce vs Map+FromEntries vs for loop
(version: 0)
Comparing performance of:
reduce vs map + fromEntries vs for loop
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = Array.from(Array(10000).keys());
Tests:
reduce
Object.entries(data).reduce((acc, [k, v]) => { acc[k] = v.toString(); return acc; }, {});
map + fromEntries
Object.fromEntries(Object.entries(data).map(([k, v]) => ([k, v.toString()])));
for loop
const dataObject = Object.entries(data) const acc = {} for (let i = 0; i < dataObject.length; i++) { const [k, v] = dataObject[i]; acc[k] = v.toString() }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
reduce
map + fromEntries
for loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:148.0) Gecko/20100101 Firefox/148.0
Browser/OS:
Firefox 148 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
3345.3 Ops/sec
map + fromEntries
2627.6 Ops/sec
for loop
3557.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark compares the performance of three approaches for iterating over an object and converting its values to strings: `reduce`, `map + fromEntries`, and a traditional `for loop`. **Tested Options** 1. **Reduce**: Uses `Array.prototype.reduce()` method to iterate over the object's entries and accumulate the converted values. 2. **Map + FromEntries**: Utilizes `Array.prototype.map()` to convert each value to a string, followed by `Object.fromEntries()` to create an object from the array of key-value pairs. 3. **For Loop**: Manually iterates over the object's entries using a traditional `for` loop. **Pros and Cons** * **Reduce**: + Pros: Compact code, easy to read. + Cons: May be slower due to the overhead of recursive accumulation. * **Map + FromEntries**: + Pros: Clear and concise code, avoids recursion. + Cons: Requires two additional function calls, which can introduce overhead. * **For Loop**: + Pros: Control over iteration flow, no additional function calls. + Cons: More verbose code, requires manual index management. **Library/Functionality** The `map()` and `fromEntries()` functions are part of the ECMAScript standard and are supported by most modern browsers. However, `reduce()` is also a standard function, but it's worth noting that some older versions of Internet Explorer may not support it. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes used in this benchmark. **Other Alternatives** If you prefer a different approach, here are some alternatives: * **Lodash's `reduceBy()`**: A utility function from the Lodash library that provides a more concise and readable way to reduce an object by a key. * **Array.prototype.forEach() with map() and spread operator**: Another alternative approach using `forEach()` to iterate over the object, followed by `map()` to convert values and the spread operator (`...`) to create an object from the array. Keep in mind that these alternatives may not be relevant for this specific benchmark, but they can provide additional options for similar use cases.
Related benchmarks:
reduce (immutable) vs map + fromEntries
Object.fromEntries vs reduce vs Map + Array.from
Map & Object.fromEntries vs reduce
Object.fromEntries on array vs reduce on array
Object.fromEntries vs reduce vs Map/reduce vs new Map
Comments
Confirm delete:
Do you really want to delete benchmark?