Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
flatMap vs reduce flattern array
(version: 0)
Comparing performance of:
reduce with concat vs flatMap
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array(10_000).fill([1, 2])
Tests:
reduce with concat
arr.reduce((acc, x) => acc.concat(x), [])
flatMap
arr.flatMap(x => x)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce with concat
flatMap
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/128.0.0.0 Safari/537.36 Edg/128.0.0.0
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce with concat
62.2 Ops/sec
flatMap
4111.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark test and explain what's being tested, compared, and discussed. **Benchmark Definition** The first step is to understand what's being measured. The benchmark definition json provides two script preparation codes: 1. `arr.reduce((acc, x) => acc.concat(x), [])` 2. `arr.flatMap(x => x)` These two scripts are designed to process an array of 10,000 elements, where each element is an array containing two elements: `[1, 2]`. **What's being tested?** The test case measures the performance difference between using `reduce` with `concat` and `flatMap`. In JavaScript, both methods can be used to flatten arrays, but they work differently. **Options compared** Two options are being compared: 1. **Using `reduce` with `concat`:** * The `reduce` method applies a callback function to each element in the array, reducing it to a single value. * In this case, the callback function concatenates the elements of the inner array (`[1, 2]`) to the accumulator (`acc`). * The `concat` method creates a new array by copying the elements from one or more arrays. * This approach can be slower because it involves creating a new array on each iteration. 2. **Using `flatMap`:** * The `flatMap` method applies a callback function to each element in the array, returning an array of the results. * In this case, the callback function simply returns the inner array (`[1, 2]`) without modifying it. * This approach is often faster because it avoids creating new arrays. **Pros and Cons** Here are some pros and cons for each approach: **Using `reduce` with `concat`:** Pros: * More control over the processing of elements * Can be more flexible Cons: * Involves creating a new array on each iteration, which can be slow * May consume more memory **Using `flatMap`:** Pros: * Often faster due to reduced memory allocation and copying * Simplifies code by avoiding explicit concatenation Cons: * Less control over the processing of elements * May not be suitable for all use cases where explicit flattening is required **Other considerations** The benchmark also considers the impact of the JavaScript engine, browser, device platform, operating system, and number of executions per second. **Library and Special JS Features** There are no libraries mentioned in the provided code snippet. However, it's worth noting that some libraries like Lodash provide `flatMap` functionality as a utility function. If special JS features or syntax were used, please let me know, and I'll be happy to explain them. **Alternatives** For this specific benchmark test, there are two alternatives: 1. **Using `map` instead of `reduce`:** * The `map` method applies a callback function to each element in the array, returning an array of results. * This approach is similar to using `flatMap`, but it's not directly comparable because it doesn't flatten the inner arrays. 2. **Using other flattening methods:** * Other methods like `forEach`, `for...of` loops, or custom functions might also be used for flattening arrays. Keep in mind that these alternatives may not provide a direct comparison to the `reduce` with `concat` and `flatMap` approaches. I hope this explanation helps!
Related benchmarks:
flatMap vs reduce using push
reduce vs. flatMap v3
Reduce vs flatMap performance
Reduce Push vs. flatMap with subarrays
Comments
Confirm delete:
Do you really want to delete benchmark?