Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce vs Map + Array.from
(version: 0)
Comparing performance of:
Object.fromEntries vs Reduce (reuse object) vs Map + Array.from
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = { ...Array.from(Array(10000).keys()) };
Tests:
Object.fromEntries
Object.fromEntries(Object.entries(data).map((key, value) => [key, value]));
Reduce (reuse object)
Object.entries(data).reduce((acc, [k, v]) => { acc[k] = v.toString(); return acc; }, {});
Map + Array.from
Array.from(new Map(Object.entries(data).map((key, value) => [key, value])))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce (reuse object)
Map + Array.from
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):
**Overview** The provided JSON represents a JavaScript benchmark test case on MeasureThat.net, which compares the performance of three different approaches to create an object from key-value pairs: `Object.fromEntries`, `reduce`, and `Map + Array.from`. **Approaches Compared** 1. **`Object.fromEntries`**: This method creates an object from an array of key-value pairs. It's a relatively new addition to JavaScript, introduced in ECMAScript 2019 (ES10). 2. **`reduce`**: The `reduce` method applies a function to each element in an array and reduces it to a single value. In this case, it's used to create an object from key-value pairs by accumulating the values in an object. 3. **`Map + Array.from`**: This approach uses a `Map` data structure to store key-value pairs and then converts it to an array using `Array.from`. The array is then passed to `Object.fromEntries`. **Pros and Cons of Each Approach** 1. **`Object.fromEntries`**: * Pros: High performance, concise syntax, widely supported (ES10+). * Cons: May not be optimized for older browsers or environments without ES10 support. 2. **`reduce`**: * Pros: Widely supported, flexible syntax. * Cons: Can be slower than `Object.fromEntries`, especially with large arrays. 3. **`Map + Array.from`**: * Pros: May offer better performance in older browsers or environments without ES10 support, as it avoids the overhead of `Object.fromEntries`. * Cons: More verbose syntax, requires additional data structure creation. **Library and Special JS Features** In this benchmark, no external libraries are used. However, note that `Map` is a built-in JavaScript data structure introduced in ECMAScript 1999 (ES3), so it's not specific to modern browsers or environments. There are no special JavaScript features being tested here, as the approaches are relatively standard and widely supported. **Other Alternatives** In addition to these three approaches, other methods for creating objects from key-value pairs include: 1. **`Object.keys()` + `reduce()`**: This approach uses `Object.keys()` to get an array of keys and then reduces it to an object using the `reduce()` method. 2. **`Array.prototype.reduce()`**: Similar to the previous approach, but uses `Array.prototype.reduce()` instead. 3. **Manual iteration**: Creating an object by manually iterating over key-value pairs using a loop. These alternatives may have different performance characteristics compared to the approaches tested in this benchmark.
Related benchmarks:
reduce (immutable) vs map + fromEntries
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
Comments
Confirm delete:
Do you really want to delete benchmark?