liveBook · Manning

Method fusion

As explained in Functional Programming in JavaScript

A functional-programming optimization that combines compatible operations into fewer passes and fewer intermediate data structures.

Method fusion combines a planned sequence of functional operations so execution can avoid unnecessary work. In Lodash, chaining separates the sequence from execution, allowing lazy evaluation and shortcut fusion. section 7.2

Fuse a mapped and filtered sequence

const result = _.chain(_.range(200)) .map(x => Math.pow(x, 2)) .filter(x => x % 2 === 0) .take(3) .value(); // [0, 4, 16] The chain describes the operations first and runs them only when value() is called. Because take(3) limits demand, the sequence need not eagerly produce every intermediate result.

How Functional Programming in JavaScript covers it

How deep it goes
Demonstrates a Lodash chain and contrasts eager and lazy range processing, but does not detail fusion internals.
Examples
Demonstrates Lodash range, map, filter, take, and value operations over a sequence of numbers.
What it assumes
Assumes JavaScript eager evaluation and familiarity with Lodash's _.chain and terminating value() operation.
What you can do after
Apply shortcut fusion through a Lodash lazy chain that delays execution until value() and limits demanded results.
How it connects
Connects lazy evaluation, eager evaluation, function composition, functional combinators, and Lodash chaining.

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 Functional Programming in JavaScript. The section links point to the parts of the book it draws on.