Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map & Object.fromEntries vs reduce
(version: 0)
Comparing performance of:
map & fromEntries vs reduce
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = Array.from(Array(10000).keys()); var f = v => v + ';';
Tests:
map & fromEntries
Object.fromEntries(data.map(x => [x, f(x)]));
reduce
data.reduce((acc, x) => ({...acc, [x]: f(x)}), {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
map & 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/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
map & fromEntries
2947.0 Ops/sec
reduce
178.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark JSON and explain what's being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark is comparing two approaches: 1. Using `Object.fromEntries` with an array of key-value pairs created using `data.map()`. 2. Using `Array.prototype.reduce()` to accumulate an object with key-value pairs from the same data source. Both approaches aim to create an object from a data set, but they have different implementation details. **Options Compared** The two options are: 1. **`Object.fromEntries`**: A modern JavaScript method introduced in ECMAScript 2017, which creates an object by passing an array of key-value pairs. 2. **`Array.prototype.reduce()`**: A common method for accumulating data in an array, which can be used to create an object. **Pros and Cons** 1. **`Object.fromEntries`**: * Pros: Easy to read and write, concise syntax, modern JavaScript feature (ECMAScript 2017). * Cons: Might have performance overhead due to its inherent complexity. 2. **`Array.prototype.reduce()`**: * Pros: Mature, widely supported, and well-understood algorithm. * Cons: More verbose than `Object.fromEntries`, might lead to additional computations. **Library/Functionality** There is no specific library used in this benchmark. Both `Object.fromEntries` and `Array.prototype.reduce()` are built-in JavaScript functions. **Special JS Feature/Syntax** The benchmark uses the modern JavaScript feature `ECMAScript 2017` ( Introduced in Firefox 91), specifically `Object.fromEntries`. This feature was added to provide a concise way to create objects from arrays of key-value pairs. **Other Alternatives** Alternative approaches could include: 1. **Manual object creation**: Using `Array.prototype.forEach()` or `Array.prototype.forEach.call()` to iterate over the data array and create an object. 2. **Using a library function**: Depending on the specific use case, alternative libraries like Lodash might provide optimized functions for creating objects from arrays. In conclusion, this benchmark compares two approaches to create an object from a data set: using `Object.fromEntries` with modern JavaScript feature (ECMAScript 2017) and using `Array.prototype.reduce()`. Both options have their pros and cons, and the choice ultimately depends on the specific use case, personal preference, or project requirements.
Related benchmarks:
reduce vs map + fromEntries
reduce (immutable) vs map + fromEntries
Object.fromEntries vs reduce vs Map + Array.from
Object.fromEntries on array vs reduce on array
Comments
Confirm delete:
Do you really want to delete benchmark?