Lodash's shortcut fusion with lazy evaluation doesn't optimize

Viewed 83

I'm trying to test Lodash's lazy evaluation and shortcut fusion but it seems like the shortcut fusion works for very limited cases.

For example, I'd expect the following to be performant and should've looped over the array only once/twice, but the case doesn't seems so:

_([...Array(10000000)])
.fill(100)
.filter(i => i)
.filter(i => i)
.filter(i => i)
.filter(i => i)
.filter(i => i)
.filter(i => i)
.filter(i => i)
.value();

The native chaining as follows is taking similar compute time:

[...Array(10000000)]
.fill(100)
.filter(i => i)
.filter(i => i)
.filter(i => i)
.filter(i => i)
.filter(i => i)
.filter(i => i)
.filter(i => i)

Ideally, I'd have expected lodash to simply merge all the subsequent filters into a single logically Anded operation and hence a single loop. Am I doing anything wrong or expecting a lot out of their so-called heuristic ?

Here is a perf test link: https://jsbench.me/dukl7k4rhc/1

0 Answers
Related