Skip to curriculum
Jump to the editor

8.7. Memoize with a key resolver and a size limit

Medium
Topics

This question is in

8. Currying and memoization
20–30 min

A plain memoize keys its cache by the first argument and never forgets. Both are problems in real code. A function such as price(productId, currency) needs both arguments in the key. And a cache that keeps every result forever turns into a memory leak in a page that runs for hours: a search box memoized by query would keep every query ever typed.

Write memoize(fn, options) in memoize.js. It caches fn's results under a key that a resolver function can build from all the arguments, and with a maxSize it keeps only that many entries, dropping the least recently used one when it needs room.

Signature

JAVASCRIPT
function memoize(fn, options) // returns the memoized function
  • fn: the function to memoize.
  • options (optional): an object with two optional fields.
    • resolver: a function that receives the same arguments and this as fn, and returns the cache key.
    • maxSize: the most entries to keep; Infinity when missing.
  • The memoized function takes any arguments and returns fn's result for their key.

Requirements

  1. memoize returns a function, and does not call fn itself.
  2. Each call works out a key: resolver's return value when there is a resolver, otherwise the first argument. The resolver runs on every call, with every argument and the caller's this.
  3. When the key is cached, the call returns the stored result without running fn. Otherwise fn runs with the caller's this and every argument, and its result is stored under the key and returned.
  4. Keys are compared as Map keys are: 1 and "1" differ, objects match only themselves, NaN matches NaN. A result of undefined is cached like any other.
  5. The cache never holds more than maxSize entries. Storing an entry beyond that removes the entry that was used longest ago, where a cache hit counts as a use. Up to maxSize entries, nothing is removed.
  6. An error from fn or from resolver reaches the caller, nothing is stored, and no entry is removed.
  7. maxSize that is not a number, or is below 1 or NaN, makes memoize throw a RangeError.
  8. Each memoized function has its own cache and limit.

Examples

JAVASCRIPT
const price = memoize(
  (id, currency) => lookUpPrice(id, currency),
  { resolver: (id, currency) => id + ":" + currency }
);
price(7, "INR"); // runs lookUpPrice
price(7, "USD"); // runs it again: a different key
price(7, "INR"); // cached
JAVASCRIPT
const square = memoize((n) => n * n, { maxSize: 2 });
square(1);       // runs: cache holds 1
square(2);       // runs: cache holds 1, 2
square(1);       // hit: 1 is now the most recently used
square(3);       // runs: the cache is full, so 2 (used longest ago) is removed
square(2);       // runs again
JAVASCRIPT
memoize(fn, { maxSize: 0 }); // RangeError

Environment

  • Plain JavaScript in a browser page; the tests call the global memoize from memoize.js.
  • Time-based expiry and a way to clear the cache are not part of this problem.
  • The basic version, keyed by one argument with no limit, is problem 8.6.

Hints

  1. A Map remembers the order in which keys were added. How can that order also say which key was used most recently?
  2. Which key is the first one a Map gives you?

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…