Skip to curriculum
Jump to the editor

8.6. Memoize a function

Easy
Topics

This question is in

8. Currying and memoization
10–15 min

Some functions are slow but always give the same result for the same input: parsing a date format, finding the factors of a number, computing a layout for a given width. Memoizing such a function means remembering each result the first time it is computed, and returning the remembered result on later calls with the same input, without running the function again.

Write memoize(fn) in memoize.js for functions of one argument. It returns a new function that behaves like fn, but runs fn at most once per distinct argument.

Signature

JAVASCRIPT
function memoize(fn) // returns the memoized function
  • fn: a function of one argument whose result depends only on that argument.
  • The memoized function takes one argument and returns fn's result for it.

Requirements

  1. memoize(fn) returns a function, and does not call fn itself.
  2. The first call with an argument runs fn and returns its result; later calls with the same argument return the same value (the same object, not a copy) without running fn.
  3. Arguments are compared as Map keys are: 1 and "1" are different, two objects are the same only if they are the same object, and NaN matches NaN.
  4. Falsy arguments (0, "", false, null, undefined) each get their own entry, and falsy results, undefined included, are cached like any other.
  5. If fn throws, the error reaches the caller and nothing is cached: the next call with that argument runs fn again.
  6. Each memoized function has its own cache.
  7. fn runs with the this of the call, so the memoized function works as a method. It is fn itself that runs, even when fn has its own property named call or apply.
  8. A recursive fn that calls its own memoized version benefits from the cache.

Examples

JAVASCRIPT
const slowSquare = (n) => { console.log("computing", n); return n * n; };
const square = memoize(slowSquare);

square(4); // logs "computing 4", returns 16
square(4); // returns 16, logs nothing
square(5); // logs "computing 5", returns 25
JAVASCRIPT
const kind = memoize((value) => typeof value);
kind(1);   // "number"
kind("1"); // "string": a different argument
JAVASCRIPT
const fib = memoize((n) => (n < 2 ? n : fib(n - 1) + fib(n - 2)));
fib(90);   // 2880067194370816000, with 91 runs instead of billions

Environment

  • Plain JavaScript in a browser page; the tests call the global memoize from memoize.js.
  • Only the first argument matters. Several arguments, a custom key and a size limit are problem 8.7.

Hints

  1. Which built-in collection keeps 1 and "1" apart, and compares objects by identity?
  2. If a stored result is undefined, how do you tell "stored" from "not stored"?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…