Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce vs property assignment
(version: 0)
Comparing performance of:
Object.fromEntries vs Reduce vs Property Assignment
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var keys = Array(10000).fill(null).map((val, index) => index);
Tests:
Object.fromEntries
Object.fromEntries(keys.map((key) => [key, null]));
Reduce
keys.reduce((acc, k) => { acc[k] = null; return acc; }, {});
Property Assignment
const newObj = {}; keys.forEach((k) => { newObj[k] = null; });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce
Property Assignment
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:132.0) Gecko/20100101 Firefox/132.0
Browser/OS:
Firefox 132 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.fromEntries
4308.3 Ops/sec
Reduce
12404.6 Ops/sec
Property Assignment
14020.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript benchmark test case on the MeasureThat.net website. The test aims to compare three different approaches for creating an object with key-value pairs: `Object.fromEntries`, `Array.prototype.reduce()`, and simple property assignment. **Test Case Options:** 1. **`Object.fromEntries(keys.map((key) => [key, null]))`**: This method creates a new object by passing an array of key-value pairs to the `fromEntries()` function. 2. **`keys.reduce((acc, k) => { acc[k] = null; return acc; }, {})`**: This method uses the `reduce()` function to iterate over the array and assign each value to an existing property in the accumulator object (`acc`). If a key is not present in the accumulator, it creates a new property. 3. **Simple Property Assignment: `const newObj = {}; keys.forEach((k) => { newObj[k] = null; });`**: This method simply iterates over the array and assigns each value to a new property in an empty object (`newObj`). **Pros and Cons of Each Approach:** 1. **`Object.fromEntries()`**: * Pros: + Concise and readable syntax. + Can create objects with default values for missing keys. * Cons: + May have performance implications due to the overhead of creating an object iterator. 2. **`Array.prototype.reduce()`**: * Pros: + Can be more efficient than `Object.fromEntries()` in some cases, especially when dealing with large arrays. + Allows for flexible accumulator initialization. * Cons: + May have a steeper learning curve due to the function signature and callback pattern. 3. **Simple Property Assignment**: * Pros: + Lightweight and easy to understand syntax. + No performance overhead from creating an object iterator. * Cons: + Requires manual property access and management, which can be error-prone. **Library Used:** None of the test cases explicitly use any external libraries. However, `Object.fromEntries()` relies on the built-in `Object.fromEntries()` method introduced in ECMAScript 2019 (ES2020). **Special JS Feature/Syntax:** There is no special JavaScript feature or syntax used in these test cases. **Other Alternatives:** For creating objects with key-value pairs, other approaches might include: 1. Using a library like Lodash (`_.fromEntries()`) for more robust and flexible object creation. 2. Utilizing the `Array.prototype.forEach()` method with an object literal to create a new object property on each iteration (similar to Simple Property Assignment). 3. Employing other methods, such as `Array.prototype.map()` or `Array.prototype.filter()`, in combination with `Object.assign()` or other object creation functions. Keep in mind that the choice of approach often depends on the specific use case, performance requirements, and personal preference.
Related benchmarks:
reduce (immutable) vs map + fromEntries
Map & Object.fromEntries vs reduce
Object.fromEntries vs reduce vs property assignment vs Map
Object.fromEntries on array vs reduce on array
Object.fromEntries vs reduce round 2
Comments
Confirm delete:
Do you really want to delete benchmark?