liveBook · Manning

Method fusion

As explained in The Joy of JavaScript

An optimization technique that combines multiple chained operations into fewer passes over a data structure to reduce allocation and overhead.

Method fusion, or shortcut fusion, combines chained operations into fewer passes over a data structure. In JavaScript, replacing map(...).map(...) with one map and a composed function avoids intermediate arrays, reducing memory allocation and execution overhead. section 5.4.2

Fusing array transformations

These forms produce the same result, but the composed version performs one map instead of three and avoids multiple array copies: ['aa','bb','cc'].map(compose(toUpper, join, unique)); ['aa','bb','cc'].map(unique).map(join).map(toUpper); section 5.4.2

['aa','bb','cc'].map(
     compose(
       toUpper,
       join,
       unique
    )
 );

Use the composed array transformation as a compact illustration of fusing several map operations into one pass. section 5.4.2

How The Joy of JavaScript covers it

How deep it goes
Explains the allocation tradeoff and composition technique, but stops short of detailed runtime implementation.
Examples
Uses composed versus chained array maps, plus flatMap replacing separate map and flat calls.
What it assumes
Assumes familiarity with JavaScript arrays, map, flatMap, function composition, and monadic terminology.
What you can do after
Apply method fusion by composing transformations or choosing flatMap to avoid unnecessary intermediate arrays.
How it connects
Connects method fusion to map, compose, flatMap, monads, functors, and lazy evaluation.

Also explained in

Compare how 2 books explain method fusion →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Joy of JavaScript. The section links point to the parts of the book it draws on.