Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
fromEntries vs for vs reduce
(version: 0)
Object creation from an array
Comparing performance of:
Object.fromPairs vs Array.reduce(...) vs for loop
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var xs = [ ["foo1", "bar"], ["foo2", "bar"], ["foo3", "bar"], ["foo4", "bar"], ["foo5", "bar"], ["foo6", "bar"], ["foo7", "bar"], ["foo8", "bar"], ["foo9", "bar"], ["foo10", "bar"], ["foo11", "bar"], ["foo12", "bar"], ];
Tests:
Object.fromPairs
Object.fromEntries(xs)
Array.reduce(...)
xs.reduce((agg, [k, v]) => ({ ...agg, [k]: v }), {});
for loop
let r = {}; for (let i = 0; i < xs.length; i++) { r[xs[i][0]] = xs[i][1]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Object.fromPairs
Array.reduce(...)
for loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:121.0) Gecko/20100101 Firefox/121.0
Browser/OS:
Firefox 121 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object.fromPairs
2534788.0 Ops/sec
Array.reduce(...)
870770.4 Ops/sec
for loop
11366692.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark compares three approaches to create an object from an array: 1. `Object.fromEntries(xs)`: This method was introduced in ECMAScript 2019 (ES10). It takes an iterable of key-value pairs as input and returns a new object with the key-value pairs. 2. For loop: A traditional for loop that iterates over the array and assigns values to an empty object using the syntax `r[xs[i][0]] = xs[i][1];`. 3. `Array.reduce((agg, [k, v]) => ({ ...agg, [k]: v }), {})`: This method uses the reduce() function to iterate over the array and accumulate a new object with key-value pairs. **Comparison** The benchmark compares these three approaches in terms of performance. The test cases are: * `Object.fromEntries(xs)` * For loop * `Array.reduce((agg, [k, v]) => ({ ...agg, [k]: v }), {})` **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **`Object.fromEntries(xs)`**: * Pros: Concise syntax, easy to read, and maintain. * Cons: May not be supported in older browsers or environments that don't support ES10. 2. For loop: * Pros: Widely supported across all browsers and environments. * Cons: Longer syntax, may lead to more errors due to the need for manual index management. 3. `Array.reduce((agg, [k, v]) => ({ ...agg, [k]: v }), {})`: * Pros: Can be used with other reduce() functions, making it a versatile option. * Cons: May require additional setup and understanding of the reduce() function. **Library/Lodash** In the benchmark definition, there is no explicit library mentioned. However, in some test cases, `Lodash` might be used implicitly through its features, like `reduce()`. If you're using Lodash, it provides a `map()` method that can also be used to create an object from an array. **Special JS feature/Syntax** There is no special JavaScript feature or syntax mentioned in this benchmark. All code snippets use standard ECMAScript syntax. **Alternatives** If the `Object.fromEntries(xs)` approach doesn't work for your environment, you could consider using other methods, like: 1. `Array.prototype.map()`: Creates a new array with transformed values. 2. `Array.prototype.forEach()`: Iterates over the array and executes a callback function for each element. 3. Manual iteration using `for` loop or `while` loop. Keep in mind that these alternatives might not be as efficient as `Object.fromEntries(xs)` if performance is critical.
Related benchmarks:
Spread vs Array.splice
slice() vs spread operator
arr.slice() vs [...arr]
slice vs new array
array.length = 0 vs []
Comments
Confirm delete:
Do you really want to delete benchmark?