Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce v2
(version: 0)
Comparing performance of:
Object.fromEntries vs Reduce (mutate) vs Reduce (spread)
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = [...Array(10000)].map((_, i) => [i, i]);
Tests:
Object.fromEntries
Object.fromEntries(data);
Reduce (mutate)
data.reduce((acc, [k, v]) => { acc[k] = v; return acc; }, {});
Reduce (spread)
data.reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.fromEntries
Reduce (mutate)
Reduce (spread)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36 Edg/120.0.0.0
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.fromEntries
1815.7 Ops/sec
Reduce (mutate)
16638.6 Ops/sec
Reduce (spread)
53.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript benchmarks. **Benchmark Overview** The benchmark tests three different approaches to creating an object from an array of key-value pairs: 1. `Object.fromEntries()` 2. `Array.prototype.reduce()` with mutation (assigning values to existing properties) 3. `Array.prototype.reduce()` with spreading (creating a new object and assigning values) **Options Compared** The benchmark compares the performance of these three approaches on a dataset containing 10,000 key-value pairs. * `Object.fromEntries()`: A modern JavaScript method introduced in ECMAScript 2017 that creates an object from an array of key-value pairs. * `Array.prototype.reduce()` with mutation: This approach uses the reduce method to iterate over the array and assign values to existing properties on the accumulator object. The mutate option indicates that the original array is modified during the execution. * `Array.prototype.reduce()` with spreading: Similar to the previous approach, but instead of modifying the original array, a new object is created using the spread operator (`{ ...acc, [k]: v }`). **Pros and Cons** Here's a brief summary of each approach: * **Object.fromEntries()**: + Pros: Modern, concise, and efficient. It uses the `Map` internal object under the hood, which provides good performance. + Cons: May not work in older browsers or environments that don't support ECMAScript 2017. * **Array.prototype.reduce()` with mutation**: + Pros: Works in all browsers and environments that support JavaScript. The mutate option can be useful when working with large datasets. + Cons: Can be slower than `Object.fromEntries()` due to the overhead of iterating over the array and modifying its properties. * **Array.prototype.reduce()` with spreading**: + Pros: Similar performance characteristics to `Object.fromEntries()`. It's a good option when working with large datasets, as it avoids modifying the original array. + Cons: May have slightly higher overhead due to the creation of a new object. **Library and Purpose** None of the tested libraries are explicitly mentioned in the benchmark definition. However, `Object.fromEntries()` uses the `Map` internal object, which is a built-in JavaScript object that provides efficient key-value storage. **Special JS Feature or Syntax** The benchmark doesn't use any special JavaScript features or syntax. It only relies on standard JavaScript methods and properties. **Alternatives** Other alternatives for creating an object from an array of key-value pairs include: * Using the `Array.prototype.reduce()` method with a callback function that creates a new object using the spread operator (`({ [k]: v })`) * Utilizing libraries like Lodash or Ramda, which provide optimized implementations of these methods * Leveraging the `Object.create()` method in combination with the spread operator to create an object from an array
Related benchmarks:
reduce (immutable) vs map + fromEntries
Map & Object.fromEntries vs reduce
Object.fromEntries on array vs reduce on array
Object.fromEntries vs reduce round 2
fromEntries vs reduce fight!
Comments
Confirm delete:
Do you really want to delete benchmark?