Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object.fromEntries vs reduce 2.1
(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:
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's being tested. **Benchmark Overview** The benchmark is comparing three approaches to create an object from an array of key-value pairs: 1. `Object.fromEntries(data)` 2. `data.reduce((acc, [k, v]) => {\r\n acc[k] = v;\r\n return acc;\r\n}, {})` 3. `data.reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {})` **Options Comparison** The three options are being compared in terms of performance, specifically the number of executions per second. * `Object.fromEntries(data)`: This method is a modern addition to JavaScript (introduced in ECMAScript 2015), which creates an object from an array of key-value pairs. It's designed to be fast and efficient. * `data.reduce((acc, [k, v]) => {\r\n acc[k] = v;\r\n return acc;\r\n}, {})`: This method uses the Array.prototype.reduce() method to iterate over the array and create an object. The callback function takes each key-value pair from the array and adds it to the accumulator object. * `data.reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {})`: This method is similar to the previous one but uses spread notation (`{ ...acc, [k]: v }`) to create a new object for each iteration. The callback function takes each key-value pair from the array and adds it to the accumulator object. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * `Object.fromEntries(data)`: + Pros: Fast, efficient, modern, and widely supported. + Cons: May not be as flexible or customizable as other approaches. * `data.reduce((acc, [k, v]) => {\r\n acc[k] = v;\r\n return acc;\r\n}, {})`: + Pros: Flexible, customizable, and well-established. + Cons: Can be slower than modern methods like `Object.fromEntries()` due to the need for manual iteration. * `data.reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {})`: + Pros: Similar to the previous approach but with spread notation, which can make the code more concise and readable. + Cons: Still requires manual iteration and may be slower than modern methods. **Library and Special JS Features** There are no libraries being used in this benchmark. However, `Object.fromEntries()` is a special feature introduced in ECMAScript 2015, making it a modern JavaScript feature that's widely supported across different browsers and environments. **Other Alternatives** If you're looking for alternative approaches to create an object from an array of key-value pairs, some other options include: * Using the Array.prototype.forEach() method with an arrow function or a regular function to iterate over the array. * Using a library like Lodash (which includes a `mapObject` function) or Moment.js (which includes a `fromEntries` function). * Creating a custom function using a for...of loop or a while loop to iterate over the array. Keep in mind that these alternatives may have different performance characteristics and might require more manual effort to implement.
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?