Transducing
Let’s consider a performance problem in JavaScript that happens when we’re dealing with large arrays and applying several map()
, filter()
, or reduce()
operations. If you start with an array and apply these operations (via chaining, as we saw earlier in this chapter), you get the desired result. However, many intermediate arrays are created, processed, and discarded—and that causes delays. If you are dealing with small arrays, the extra time won’t make an impact, but with larger arrays (as in a big data process, maybe in Node.js, where you’re working with the results of a large database query), then you will probably have to need some optimization. We’ll do this by learning about a new tool for composing functions: transducing.
First, let’s create some functions and data. We’ll make do with a nonsensical example since we aren’t focusing on the actual operations but on the general process. We’ll start...