Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
flatMap vs reduce (concat) vs reduce (push)
(version: 0)
Comparing performance of:
Reduce concat vs FlatMap vs Reduce push
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array(10_000).fill(1)
Tests:
Reduce concat
arr.reduce((acc, x, index) => acc.concat(x, index % 2 ? x : []), [])
FlatMap
arr.flatMap((x, index) => [x, index % 2 && x]).filter(Boolean)
Reduce push
arr.reduce((acc, x, index) => { acc.push(x); if(index % 2) { acc.push(x) } return acc; }, [])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Reduce concat
FlatMap
Reduce push
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/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Reduce concat
8.6 Ops/sec
FlatMap
3188.7 Ops/sec
Reduce push
35196.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark measures the performance of three different approaches to concatenate arrays: `reduce` with concatenation, `flatMap`, and `reduce` with `push`. The `Script Preparation Code` defines an array `arr` with 10,000 elements, all filled with the value `1`. This array is used as input for each benchmark. **Test Cases** There are three test cases: 1. **Reduce concat**: This test case uses the `reduce` method to concatenate two arrays. The callback function takes three arguments: `acc`, `x`, and `index`. It concatenates `x` with `acc` using the comma operator (`acc.concat(x, index % 2 ? x : [])`). The result is then returned. 2. **FlatMap**: This test case uses the `flatMap` method to create a new array with transformed elements. The callback function takes two arguments: `x` and `index`. It returns an array containing `x` and either `x` or an empty array, depending on whether `index` is even. 3. **Reduce push**: This test case uses the `reduce` method to push elements onto an accumulator array. The callback function takes three arguments: `acc`, `x`, and `index`. It pushes `x` onto `acc` using the `push` method. If `index` is even, it also pushes `x` again onto `acc`. **Library Used** The `flatMap` method uses the Array.prototype.flatMap() method, which was introduced in ECMAScript 2019. **Special JS Feature/Syntax** None of the test cases use any special JavaScript features or syntax that are not widely supported. However, it's worth noting that the `flatMap` method is a relatively new addition to the language, and its behavior might be affected by certain optimizations or compiler settings. **Pros and Cons of Different Approaches** Here are some pros and cons of each approach: 1. **Reduce concat**: * Pros: Simple and efficient for small arrays. * Cons: Inefficient for large arrays due to the overhead of concatenation. 2. **FlatMap**: * Pros: More concise and expressive than `reduce` with concatenation. * Cons: Can be slower due to the creation of a new array. 3. **Reduce push**: * Pros: Similar performance to `flatMap`, but might be slightly faster due to the use of `push`. * Cons: Less concise and expressive than `flatMap`. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: 1. **Array.prototype.flat()**: This method is similar to `flatMap`, but it returns a flattened array instead of an array of arrays. 2. **Array.prototype.map()` + `Array.prototype.concat()`: You can use the `map` method to transform elements and then concatenate them using the `concat` method. Keep in mind that the performance differences between these alternatives might be significant, depending on the size and structure of your data.
Related benchmarks:
flatMap vs reduce using push
flatMap vs reduce using push spread
flat map vs reduce concat
flat map vs reduce concat for real
flatMap vs reduce flattern array
Comments
Confirm delete:
Do you really want to delete benchmark?