Why is `Array.flatMap` so slow?

But saying "flatMap should be as performant as the iterative loop" is not the same as thinking that the compiler can step in and transform code beyond what is practical. There are many ways to solve this problem.

The clear difference between your iterative and declarative implementations is not the functional style. It is that in the imperative version you, the programmer, are adding extra information: you know the final size of the array and you're reserving it up front.

In this case, it is theoretically (though not practically) possible for the compiler to deduce this information. But in many cases, this wouldn't even be possible. Maybe the mapping function is complex, or is non-deterministic. Maybe it's an opaque function call.

A different approach would be to add a size hint to the flat map call. This could be useful pattern for other similar transformations like filter or joined, where the user has better knowledge of the final length or perhaps is willing to expend more memory that they might not end up using to get a performance boost.

4 Likes