Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
index by id: Object.fromEntries vs reduce
(version: 0)
Comparing performance of:
Object.fromEntries vs Array.prototype.reduce
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = Array.from({ length: 1000 }, () => ({ id: Math.random() * 100000 | 0, key1: Math.random() * 100000 | 0, key2: Math.random() * 100000 | 0, key3: Math.random() * 100000 | 0, key4: Math.random() * 100000 | 0 }))
Tests:
Object.fromEntries
Object.fromEntries(array.map(item => [item.id, item]))
Array.prototype.reduce
array.reduce((acc, val) => { acc[val.id] = val; return acc; }, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.fromEntries
Array.prototype.reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.fromEntries
8003.6 Ops/sec
Array.prototype.reduce
10212.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **What is being tested?** The provided JSON represents a benchmark test that compares two approaches for creating an index object from an array: 1. `Object.fromEntries()` 2. `Array.prototype.reduce()` In more detail, the test creates an array of 1000 objects with random `id`, `key1`, `key2`, and `key4` properties. The benchmark then measures the execution time of two functions that create an index object from this array using the above methods. **Options compared:** The two options being compared are: 1. **Object.fromEntries()**: a modern JavaScript method introduced in ECMAScript 2015 (ES6) for creating an object from an array of key-value pairs. 2. **Array.prototype.reduce()**: a built-in method in the Array prototype that applies a function to each element in the array, accumulating a value. **Pros and cons:** * **Object.fromEntries():** + Pros: - Concise syntax - Modern and widely supported - Can be more readable for those familiar with this method + Cons: - May have performance overhead due to its creation of an object and pushing elements into it - May not be optimized for older browsers or environments * **Array.prototype.reduce():** + Pros: - Well-established and widely supported - Can be more efficient for large arrays, as it avoids creating a new object - Allows for more control over the accumulation process + Cons: - More verbose syntax compared to `Object.fromEntries()` - May require more cognitive effort from developers familiar with this method **Library usage:** None of the libraries are used in this benchmark. The functions being tested (`Object.fromEntries()` and `Array.prototype.reduce()`) are part of the JavaScript language itself. **Special JS feature or syntax:** There is no specific JavaScript feature or syntax mentioned that's not commonly known. However, it's worth noting that `Object.fromEntries()` relies on a modern ECMAScript implementation that supports this method, whereas older browsers may not support it. **Other alternatives:** If you need to create an index object from an array, other alternatives could include: 1. Using the spread operator (`...`) and object literals to create a new object. 2. Utilizing libraries like Lodash or Ramda for utility functions that can help with indexing. 3. Implementing your own loop-based solution. However, `Object.fromEntries()` is likely to be the most efficient and convenient choice in this scenario, given its concise syntax and widespread support. I hope this explanation helps you understand the test case!
Related benchmarks:
Object.fromEntries vs create temp object vs Array.reduce
Object.keys(object).includes(key) vs key in object
Object.fromEntries vs reduce, using arrays
Object.fromEntries(Array.map) vs Array.reduce (with different methods)
Comments
Confirm delete:
Do you really want to delete benchmark?