Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce vs property assignment vs Map
(version: 0)
Comparing performance of:
Object.fromEntries vs Reduce vs Property Assignment vs Map
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; });
Map
const newMap = new Map(); keys.forEach((k) => { newMap.set(k, null); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce
Property Assignment
Map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Android 13; Mobile; rv:142.0) Gecko/142.0 Firefox/142.0
Browser/OS:
Firefox Mobile 142 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.fromEntries
2717.0 Ops/sec
Reduce
5947.1 Ops/sec
Property Assignment
5326.8 Ops/sec
Map
1716.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a benchmark test for JavaScript microbenchmarks on the MeasureThat.net website. The test compares four different approaches to create an object with key-value pairs: `Object.fromEntries`, `reduce`, property assignment using destructuring, and `Map`. **Approaches Compared:** 1. **`Object.fromEntries`**: This method creates a new object from an array of key-value pairs. 2. **`reduce`**: This method applies a callback function to each element in the array, accumulating a value that is returned as the final result. 3. **Property Assignment** (also known as destructuring assignment): This approach uses the syntax `const newObj = {}; keys.forEach((k) => { newObj[k] = null; });` to create an object with key-value pairs. 4. **`Map`**: A built-in JavaScript data structure that stores key-value pairs in a hash table. **Pros and Cons of Each Approach:** * **`Object.fromEntries`**: + Pros: concise, readable syntax, efficient performance. + Cons: may be slower than other approaches for very large datasets due to the overhead of creating an object from an array of key-value pairs. * **`reduce`**: + Pros: flexible, can be used with various callback functions, efficient performance. + Cons: may have a steeper learning curve due to its functional programming nature, can lead to unexpected side effects if not used carefully. * **Property Assignment (Destructuring Assignment)**: + Pros: concise, readable syntax, efficient performance. + Cons: may be slower than other approaches for very large datasets due to the overhead of creating an object with key-value pairs using destructuring assignment. * **`Map`**: + Pros: efficient performance, allows for iteration over key-value pairs in O(1) time complexity. + Cons: requires more memory allocation and deallocation compared to other approaches. **Library Used:** None **Special JS Features/Syntax:** None mentioned The benchmark test is designed to measure the performance of each approach on a large dataset (10000 elements). The results indicate that **`Object.fromEntries`** and Property Assignment are the fastest approaches, followed closely by `Map`. However, it's essential to note that the actual performance differences may vary depending on specific use cases and datasets. **Other Alternatives:** If you need an alternative approach, you can consider using: * **`Array.prototype.forEach()`**: This method iterates over each element in the array and performs a callback function. * **`Array.prototype.reduce()`**: Similar to `reduce`, but returns an accumulator value instead of creating an object. Keep in mind that these alternatives may have different performance characteristics and use cases compared to the approaches tested in this benchmark.
Related benchmarks:
reduce (immutable) vs map + fromEntries
Object.fromEntries vs reduce vs Map + Array.from
Map & Object.fromEntries vs reduce
Object.fromEntries on array vs reduce on array
Comments
Confirm delete:
Do you really want to delete benchmark?