Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce vs for loop
(version: 0)
Comparing performance of:
Object.fromEntries vs Reduce (creating temporary objects) vs For loop
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = Array.from(Array(10000).keys());
Tests:
Object.fromEntries
Object.fromEntries(data.map((key) => [key, 'x']));
Reduce (creating temporary objects)
data.reduce((acc, k) => ({ ...acc, [k]: 'x' }), {});
For loop
const obj = {}; for (const d of data) { obj[d] = 'x'; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce (creating temporary objects)
For loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:147.0) Gecko/20100101 Firefox/147.0
Browser/OS:
Firefox 147 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.fromEntries
5128.3 Ops/sec
Reduce (creating temporary objects)
0.2 Ops/sec
For loop
13724.1 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 Overview** The benchmark is comparing three approaches to create an object with key-value pairs: `Object.fromEntries`, `reduce` (creating temporary objects), and a traditional `for` loop. **Options Compared** 1. **`Object.fromEntries`**: This method creates an object from an array of key-value pairs. 2. **`Reduce` (creating temporary objects)**: This method uses the reduce function to create an object by iteratively adding key-value pairs. 3. **Traditional `for` loop**: This approach uses a traditional loop to iterate over the data and add key-value pairs to an object. **Pros and Cons of Each Approach** 1. **`Object.fromEntries`**: * Pros: Concise, efficient, and readable code. * Cons: Requires modern JavaScript versions (ECMAScript 2017+). 2. **`Reduce` (creating temporary objects)**: * Pros: Can be more memory-efficient than `Object.fromEntries` by reusing the accumulator object. * Cons: May have performance implications due to object creation and mutation. 3. **Traditional `for` loop**: * Pros: Widely supported, no dependencies on modern JavaScript versions or libraries. * Cons: More verbose and potentially less efficient than other approaches. **Library Used** None explicitly mentioned in the benchmark definition, but it's likely that the `Array.from()` method is used, which is a built-in JavaScript method. **Special JS Feature/Syntax** There is no special feature or syntax being tested here; all approaches use standard JavaScript constructs. However, if we consider the usage of `Object.fromEntries`, it does demonstrate a modern JavaScript feature (ECMAScript 2017+) that might be interesting to some readers. **Alternative Approaches** Other alternatives for creating an object with key-value pairs could include: * Using the spread operator (`{ ... }` or `Object.assign()`) * Utilizing libraries like Lodash's `mapObject()` function * Implementing a custom solution using JavaScript iterables and objects Keep in mind that these alternative approaches may have their own pros and cons, depending on the specific use case and performance requirements.
Related benchmarks:
Object.fromEntries vs create temp object vs Array.reduce
Map & Object.fromEntries vs reduce
Object.fromEntries on array vs reduce on array
Object.fromEntries vs reduce round 2
Object.fromEntries VS multiple reduce from list of data
Comments
Confirm delete:
Do you really want to delete benchmark?