Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce vs reduce with spread
(version: 0)
Comparing performance of:
Object.fromEntries vs reduce vs reduce with spread
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array.from({ length: 100000 }).map((_, index) => index);
Tests:
Object.fromEntries
Object.fromEntries(arr.map(i => [i, i*2]))
reduce
arr.reduce((acc, curr) => { acc[curr] = curr * 2; return acc }, {})
reduce with spread
arr.reduce((acc, curr) => { return { ...acc, [curr]: curr * 2, } }, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.fromEntries
reduce
reduce with 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 is being tested, the different approaches compared, their pros and cons, and other considerations. **Benchmark Purpose** The benchmark compares three approaches to create an object from an array of key-value pairs: 1. `Object.fromEntries(arr.map(i => [i, i*2]))` 2. `arr.reduce((acc, curr) => {\r\n acc[curr] = curr * 2;\r\n return acc\r\n}, {})` 3. `arr.reduce((acc, curr) => {\r\n return {\r\n ...acc,\r\n [curr]: curr * 2,\r\n }\r\n}, {})` The goal is to determine which approach is the fastest. **Approaches Compared** 1. **Object.fromEntries**: This method was introduced in ECMAScript 2015 (ES6) as a way to create an object from an iterable of key-value pairs. 2. **Reduce with spread (`...`)**: This approach uses the `reduce()` method and the spread operator (`...`) to construct the object. **Pros and Cons** 1. **Object.fromEntries**: * Pros: + More concise and expressive code + Can be faster due to optimized internal implementation * Cons: + May not work as expected in older browsers or environments that don't support ES6 features 2. **Reduce with spread (`...`)**: * Pros: + Works in all browsers and environments that support JavaScript + Can be more flexible and customizable * Cons: + Requires more code to achieve the same result **Other Considerations** * The `reduce()` method is a powerful tool in JavaScript, but it can also be slower than other methods due to its overhead. * The spread operator (`...`) was introduced in ECMAScript 2015 (ES6) and allows for concise object creation, but it may not be supported in older browsers or environments. **Library** There is no explicit library mentioned in the benchmark definition. However, the `reduce()` method is a built-in JavaScript function that works with any array-like object. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark. To put these results into perspective, here's what they mean: * **ExecutionsPerSecond**: This value represents the average number of executions per second for each test case. The higher value indicates faster performance. * The top result (`arr.reduce((acc, curr) => {\r\n acc[curr] = curr * 2;\r\n return acc\r\n}, {})`) indicates that the "Reduce with spread" approach is the fastest, followed closely by `Object.fromEntries`. Other alternatives for this benchmark could include: * Using a different JavaScript engine or interpreter * Increasing or decreasing the size of the input array (`arr`) * Adding more tests to compare other approaches (e.g., using `forEach()` instead of `reduce()`) * Profiling the code with additional tools, such as V8's built-in profiling API.
Related benchmarks:
ES6 spread operator vs. Array.prototype.reduce()
flatMap vs reduce using push
flatMap vs reduce using push spread
Object.fromEntries on array vs reduce on array
Bench flat map vs spread reduceqwe
Comments
Confirm delete:
Do you really want to delete benchmark?