liveBook · Manning

Eager evaluation

As explained in Functional Programming in JavaScript

Eager evaluation is an execution strategy that computes expressions or performs operations immediately rather than deferring them until their results are needed.

Eager evaluation computes an expression as soon as it is bound to a variable, even when no later operation needs its result. This can waste work when a large result is immediately reduced to a small subset. section 7.2

An unnecessary function call

In Maybe.of(student).getOrElse(createNewStudent()), JavaScript evaluates createNewStudent() before getOrElse decides whether it needs a fallback. A conditional avoids that call when student already exists. section 7.2

Figure 7.6. The composition of the function range (returns a list of numbers from beginning to end) with take (reads the first n elements). In eager evaluation, the range function executes completely, feeding its result to take. With lazy evaluation, the result of range never executes until the dependent operation, take, is called.

Compare eager range generation, which produces everything before take runs, with deferred generation that produces only the requested elements. section 7.2

Reduce eager-work costs by:

  • Avoiding needless computations when a result may not be required. section 7.2

How Functional Programming in JavaScript covers it

How deep it goes
Defines immediate expression evaluation and contrasts it with deferred execution, without detailing language-runtime internals.
Examples
Uses range with take, a Maybe fallback, conditional control flow, and a Lodash map-filter-take chain.
What it assumes
Assumes basic JavaScript expressions, functions, variables, conditionals, and array-processing operations.
What you can do after
Apply conditional control flow or Lodash shortcut fusion to avoid unnecessary function calls and large intermediate results.
How it connects
Connects eager evaluation with lazy evaluation, the Maybe monad, functional combinators, and shortcut fusion.

Also explained in

Compare how 5 books explain eager evaluation →

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.