Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce vs map + fromEntries
(version: 0)
Comparing performance of:
reduce vs map + fromEntries
Created:
6 years ago
by:
Registered User
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()])));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
map + fromEntries
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Browser/OS:
Chrome 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce
1702.0 Ops/sec
map + fromEntries
895.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares two approaches to transform an object into a new object with string values: using `Array.prototype.reduce()` and using `Array.prototype.map()` in combination with `Object.fromEntries()`. The input data consists of 10,000 key-value pairs, where the keys are numbers from 0 to 9,999. **Test Case 1: reduce** This test case uses the `reduce()` method to transform the object. Here's a breakdown: * **Pros:** `reduce()` is a native JavaScript method that can be optimized by engines for specific use cases. It has lower memory overhead compared to creating an intermediate array. * **Cons:** `reduce()` requires an accumulator function, which can make the code more complex and harder to read. **Test Case 2: map + fromEntries** This test case uses `Array.prototype.map()` to transform each value in the object into a string, and then passes the resulting array of key-value pairs to `Object.fromEntries()`. Here's a breakdown: * **Pros:** This approach creates an intermediate array that can be optimized by engines for specific use cases. It also avoids the accumulator function requirement of `reduce()`. * **Cons:** Creating an intermediate array requires more memory compared to using `reduce()`. * **Additional Consideration:** The `Object.fromEntries()` method is a relatively new addition to JavaScript (introduced in ES6) and might not be supported by older browsers or engines. **Library Used: Object.entries(), Object.fromEntries()** These two methods are part of the ECMAScript standard and are implemented natively in most modern browsers. They provide a convenient way to work with objects, especially when converting them into other data structures like arrays or new objects. **Special JS Feature/Syntax: None** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** If you want to use alternative approaches, here are some options: * Using `Array.prototype.forEach()` instead of `reduce()`. This would require an additional variable to store the accumulator. * Using a library like Lodash's `mapValues()` function to create an object with string values. * Using a library like Underscore.js's `map` function to transform each value in the object. Keep in mind that using libraries or alternative approaches might introduce additional overhead, especially if not optimized by engines.
Related benchmarks:
reduce (immutable) vs map + fromEntries
Object.fromEntries vs reduce vs Map + Array.from
Object.fromEntries vs create temp object vs Array.reduce
Map & Object.fromEntries vs reduce
Object.fromEntries on array vs reduce on array
Comments
Confirm delete:
Do you really want to delete benchmark?