liveBook · Manning

Memoization

As explained in Functional Programming in JavaScript

A technique that caches the results of function calls so repeated calls with the same inputs can reuse prior results instead of recomputing.

Memoization caches a function’s result under a key made from its arguments. When the function receives the same inputs again, it returns the stored result instead of recomputing it. This is most useful for pure, computationally expensive functions whose outputs stay consistent for the same inputs. section 7.3 section 7.5

A typical memoization flow is:

  • Create a cache key from the function’s arguments. section 7.3

Memoizing ROT13 and factorial

A JavaScript function can be wrapped with memoize so repeated calls reuse prior work: var rot13 = (s => s.replace(/[a-zA-Z]/g, c => String.fromCharCode((c <= 'Z' ? 90 : 122) >= (c = c.charCodeAt(0) + 13) ? c : c - 26))).memoize(); rot13('functional_js_50_off'); rot13('functional_js_50_off'); The first call computes and stores the result; the second returns it from the cache. The same idea helps recursive factorial: after factorial(100) is cached, factorial(101) can reuse that intermediate result instead of rebuilding the entire computation.

How Functional Programming in JavaScript covers it

How deep it goes
Explains argument-based cache keys, cache hits and misses, JavaScript wrapping, and recursive reuse, but not cache invalidation.
Examples
Demonstrates cached student lookup, ROT13 encoding, recursive factorial, and a composed student-search pipeline.
What it assumes
Assumes JavaScript function execution and closures, while introducing memoization through concrete functional examples.
What you can do after
Build it from scratch in JavaScript with a memoize wrapper and apply it to expensive, repeatable function calls.
How it connects
Connects memoization with referential transparency, pure functions, recursion, lazy evaluation, and functional decomposition.

Also explained in

Compare how 8 books explain memoization →

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.