Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce v3
(version: 0)
Comparing performance of:
Object.fromEntries(.map()) vs Reduce (mutate) vs Reduce (spread)
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = [...Array(10000)].map((_, i) => ({ key: i, value: i }));
Tests:
Object.fromEntries(.map())
Object.fromEntries(data.map(({ key, value }) => [key, value]));
Reduce (mutate)
data.reduce((acc, { key, value }) => { acc[key] = value; return acc; }, {});
Reduce (spread)
data.reduce((acc, { key, value }) => ({ ...acc, [key]: value }), {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.fromEntries(.map())
Reduce (mutate)
Reduce (spread)
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
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 compares the performance of three different ways to create an object from an array of key-value pairs: 1. `Object.fromEntries` with a mapped array 2. `Array.prototype.reduce` with a mutating approach (assigning directly to the accumulator) 3. `Array.prototype.reduce` with a spreading approach (using the spread operator to create a new object) **Options Compared** The three options are compared in terms of their execution time and number of executions per second. **Pros and Cons of Each Approach** 1. **`Object.fromEntries` with mapped array**: * Pros: Easy to read, concise, and expressive. * Cons: May incur additional overhead due to the use of `Object.fromEntries`, which might not be optimized for performance in older browsers or JavaScript engines. 2. **Mutating approach (Reduce)**: * Pros: Can be more efficient than creating a new object, as it modifies an existing accumulator object. * Cons: May have performance issues if the accumulator is large or has many properties, leading to slow growth and potential memory issues. 3. **Spreading approach (Reduce)**: * Pros: Creates a new object, which can be beneficial for performance in older browsers or JavaScript engines where `Object.fromEntries` might not be supported. * Cons: May incur additional overhead due to the use of the spread operator, which might not be optimized for performance. **Library and Special JS Feature** The benchmark uses the following libraries: * None explicitly mentioned, but it's likely that the browser's built-in JavaScript engine is being used. No special JS features are used in this benchmark. **Other Considerations** When choosing an approach, consider the trade-offs between code readability, performance, and memory usage. In general, if you need to create a large number of objects with many properties, the spreading approach might be more efficient. However, if you prioritize conciseness and ease of reading, `Object.fromEntries` with mapped array might be a better choice. **Alternatives** If you're not satisfied with the results or want to explore other approaches, here are some alternatives: * Using `Array.prototype.map` with `Object.create` instead of `Object.fromEntries`. * Implementing your own custom `Object.fromEntries` equivalent using a loop. * Using a different library or framework that provides better performance for this specific use case. Keep in mind that each alternative will have its pros and cons, and it's essential to weigh the trade-offs before choosing an approach.
Related benchmarks:
reduce (immutable) vs map + fromEntries
Map & Object.fromEntries vs reduce
Object.fromEntries on array vs reduce on array
Object.fromEntries vs reduce round 2
Comments
Confirm delete:
Do you really want to delete benchmark?