16 Memoization

Lesson goal Understand how memoization caches the results of a pure function, learn to write your own memoize and use R.memoizeWith, choose a cache key, speed up recursion, and account for memory cost and invalidation.

Memoization is caching a function's results by its arguments. On the first call the function computes the result and remembers it, and on a repeated call with the same arguments it returns the stored value without computing it again. The term was coined by the British researcher Donald Michie in 1968.

The cache lives in a closure: memoize returns a wrapper, and only that wrapper can reach the stored results. A Map is a convenient cache: any value can be a key, and keys are compared almost as with ===, except that NaN is considered equal to NaN.

// memoize :: (a -> b) -> (a -> b)
// for functions of one argument: the cache key is the argument itself
const memoize = (fn) => {
  // the cache lives in the closure: nothing outside can reach it
  const cache = new Map()
  return (arg) => {
    if (cache.has(arg)) {
      return cache.get(arg)
    }
    const result = fn(arg)
    cache.set(arg, result)
    return result
  }
}

// the counter shows how many times the function actually computed;
// it is a side effect, here only for illustration
let calls = 0

// countDivisors :: Number -> Number
// checks every number from 1 to n: the larger n, the longer it takes
const countDivisors = (n) => {
  calls += 1
  return R.range(1, n + 1).filter((i) => n % i === 0).length
}

const countDivisorsMemo = memoize(countDivisors)

console.log(countDivisorsMemo(360)) // 24
console.log(countDivisorsMemo(360)) // 24, from the cache
console.log(countDivisorsMemo(1000)) // 16
console.log('computations: ' + calls) // computations: 2

Only pure functions can be memoized. The cache replaces a call with the stored result, and that is safe only when the function always returns the same thing for the same arguments and changes nothing outside. If the result depends on an outside variable, the cache returns a stale value. If the function writes or sends something, repeated calls simply won't do it.

The cache key

The key decides which calls count as the same. For a single primitive argument the argument itself is the key. A Map compares objects by reference: an object with the same contents but created anew won't be found, while an object changed in place will be found together with a stale result. That is why memoization goes well with the immutable data from the "Immutability" lesson: a new value means a new object and a new key.

// memoize from the previous example
// memoize :: (a -> b) -> (a -> b)
const memoize = (fn) => {
  const cache = new Map()
  return (arg) => {
    if (cache.has(arg)) {
      return cache.get(arg)
    }
    const result = fn(arg)
    cache.set(arg, result)
    return result
  }
}

// === an impure function: the result depends on an outside variable ===
let rate = 90
// toRub :: Number -> Number
const toRub = (usd) => usd * rate
const toRubMemo = memoize(toRub)

console.log(toRubMemo(10)) // 900
rate = 100
console.log(toRub(10)) // 1000
console.log(toRubMemo(10)) // 900: the rate has changed, but the result comes from the cache

// === an object key is compared by reference ===
let lengthCalls = 0
// nameLength :: Object -> Number
const nameLength = memoize((user) => {
  lengthCalls += 1
  return user.name.length
})

const john = { name: 'John' }
console.log(nameLength(john)) // 4
// the same contents but a different object: the cache didn't help
console.log(nameLength({ name: 'John' })) // 4
console.log('computations: ' + lengthCalls) // computations: 2

// the object was changed in place, the reference is the same: the cache returns the old answer
john.name = 'Johnny'
console.log(nameLength(john)) // 4, although the name now has 6 letters

Ramda has R.memoizeWith(keyFn, fn): the first function builds a key from the arguments, the second computes the result. The cache inside is a plain object, so the key must be a string. Anything else is converted to a string, and every object turns into the same key "[object Object]". With several arguments, the key is built with a separator: without it the calls (1, 23) and (12, 3) would give the same string "123".

let calls = 0
// area :: (Number, Number) -> Number
const area = (width, height) => {
  calls += 1
  return width * height
}

// a key built from both arguments with a separator
const areaMemo = R.memoizeWith((width, height) => width + 'x' + height, area)

console.log(areaMemo(2, 3)) // 6
console.log(areaMemo(2, 3)) // 6, from the cache
console.log(areaMemo(3, 2)) // 6, but the key is different: 3x2
console.log('computations: ' + calls) // computations: 2

// === a mistake: an object as the key ===
// R.identity returns the object itself, and as a string it becomes '[object Object]'
const getName = R.memoizeWith(R.identity, (user) => user.name)
console.log(getName({ id: 1, name: 'John' })) // John
console.log(getName({ id: 2, name: 'Anna' })) // John: someone else's result from the cache

// the key must be a string that tells the objects apart
const getNameById = R.memoizeWith((user) => String(user.id), (user) => user.name)
console.log(getNameById({ id: 1, name: 'John' })) // John
console.log(getNameById({ id: 2, name: 'Anna' })) // Anna

Recursion: Fibonacci numbers

Memoization pays off most where a function is called many times with the same arguments. The classic example is Fibonacci numbers: fib(n) = fib(n - 1) + fib(n - 2). Naive recursion computes the same values again and again, and the number of calls grows exponentially: 21,891 for fib(20) and already 2,692,537 for fib(30).

For memoization to work, the recursive calls must go through the memoized version. If you wrap a finished function from the outside, only the top call gets into the cache, and the recursion inside bypasses it.

// memoize :: (a -> b) -> (a -> b)
const memoize = (fn) => {
  const cache = new Map()
  return (arg) => {
    if (cache.has(arg)) {
      return cache.get(arg)
    }
    const result = fn(arg)
    cache.set(arg, result)
    return result
  }
}

// === naive recursion ===
let naiveCalls = 0
// fib :: Number -> Number
const fib = (n) => {
  naiveCalls += 1
  return n < 2 ? n : fib(n - 1) + fib(n - 2)
}

console.log(fib(20)) // 6765
console.log('calls: ' + naiveCalls) // calls: 21891

// === a wrapper outside: the recursion bypasses the cache ===
naiveCalls = 0
const fibWrapped = memoize(fib)
console.log(fibWrapped(20)) // 6765
// fib calls itself inside, not fibWrapped
console.log('calls: ' + naiveCalls) // calls: 21891
// only the top call is in the cache: fibWrapped(20)
console.log(fibWrapped(20)) // 6765
console.log('calls: ' + naiveCalls) // calls: 21891

// === recursion through the memoized function ===
let memoCalls = 0
// fibMemo :: Number -> Number
const fibMemo = memoize((n) => {
  memoCalls += 1
  return n < 2 ? n : fibMemo(n - 1) + fibMemo(n - 2)
})

console.log(fibMemo(20)) // 6765
// each n from 0 to 20 is computed once
console.log('computations: ' + memoCalls) // computations: 21
console.log(fibMemo(30)) // 832040
// only n from 21 to 30 were added
console.log('computations: ' + memoCalls) // computations: 31

Memory cost and invalidation

A cache takes memory and does not clean itself up. If the arguments hardly ever repeat, for example request IDs or text the user types, the cache grows with every call and saves nothing. R.memoizeWith hides its cache in a closure: you can neither limit nor clear it, you can only create a new memoized function.

Hence two techniques. The first is to limit the size. A Map remembers insertion order, so when it overflows you can delete the first entry, and move an entry to the end whenever it is accessed. This gives an LRU (least recently used) cache: it evicts whatever was accessed longest ago. The second is invalidation. If the result depends on data that changes, put the data version into the key or drop the whole cache. For object keys there is WeakMap: an entry in it doesn't stop the garbage collector from removing the object once nothing else refers to it.

// memoizeLimited :: Number -> (a -> b) -> (a -> b)
// an LRU cache of limit entries with a manual reset
const memoizeLimited = (limit) => (fn) => {
  const cache = new Map()
  const memoized = (arg) => {
    if (cache.has(arg)) {
      const value = cache.get(arg)
      // move the entry to the end: it was used most recently
      cache.delete(arg)
      cache.set(arg, value)
      return value
    }
    const result = fn(arg)
    cache.set(arg, result)
    if (cache.size > limit) {
      // the first entry in the Map is the one accessed longest ago
      cache.delete(cache.keys().next().value)
    }
    return result
  }
  // invalidation: drop the whole cache
  memoized.clear = () => cache.clear()
  memoized.size = () => cache.size
  return memoized
}

let calls = 0
// square :: Number -> Number
const square = (n) => {
  calls += 1
  return n * n
}

const squareMemo = memoizeLimited(2)(square)

squareMemo(1) // computed
squareMemo(2) // computed
squareMemo(1) // from the cache, 1 is now the most recent entry
squareMemo(3) // computed, the cache overflows: 2 is evicted
squareMemo(1) // from the cache
squareMemo(2) // 2 was evicted, computed again
console.log('computations: ' + calls + ', cached: ' + squareMemo.size()) // computations: 4, cached: 2

squareMemo.clear()
squareMemo(1) // computed again after the reset
console.log('computations: ' + calls + ', cached: ' + squareMemo.size()) // computations: 5, cached: 1

Memoization pays off when the function is pure, takes noticeably longer than building a key and looking up an entry, and is called with repeating arguments. Typical cases are recursion with overlapping subproblems, parsing and formatting the same strings, and computing derived data from state. For cheap functions like addition a cache only adds work and memory. As in the "Performance" lesson, check the gain by measuring rather than by a single run: count computations and cache hits, or repeat time measurements.

Key points

  • Memoization remembers a function's result by its arguments and, on a repeated call with the same arguments, returns it from a cache that lives in a closure.
  • Only pure functions can be memoized: otherwise the cache returns a stale result or skips a side effect.
  • The cache key decides which calls are the same: a Map compares objects by reference, R.memoizeWith(keyFn, fn) expects a string key, and for several arguments the key is built with a separator.
  • In recursion the calls must go through the memoized function: fib(20) without a cache makes 21,891 calls, and with a cache it computes 21 values.
  • A cache costs memory and doesn't clean itself up: its size is limited by evicting old entries, and when outside data changes the cache is dropped or the data version is put into the key.

Exercises

Exercise 1

paths(rows, cols) counts how many paths lead from the top-left corner of a grid to the bottom-right one if you can only move right or down. Naive recursion makes hundreds of thousands of calls for a 10 by 10 grid. Write pathsMemo with R.memoizeWith: the recursive calls must go through the cache, and the key must tell pairs of arguments apart. Compare the number of calls and computations.

Starter code

let naiveCalls = 0
// paths :: (Number, Number) -> Number
const paths = (rows, cols) => {
  naiveCalls += 1
  if (rows === 0 || cols === 0) return 1
  return paths(rows - 1, cols) + paths(rows, cols - 1)
}

console.log(paths(10, 10))
console.log('without a cache, calls: ' + naiveCalls)

let memoCalls = 0
// your code: pathsMemo with R.memoizeWith, increment memoCalls inside

console.log(pathsMemo(10, 10))
console.log('with a cache, computations: ' + memoCalls)

Expected output

184756
without a cache, calls: 369511
184756
with a cache, computations: 120
Show solution
let naiveCalls = 0
// paths :: (Number, Number) -> Number
const paths = (rows, cols) => {
  naiveCalls += 1
  if (rows === 0 || cols === 0) return 1
  return paths(rows - 1, cols) + paths(rows, cols - 1)
}

console.log(paths(10, 10))
console.log('without a cache, calls: ' + naiveCalls)

let memoCalls = 0
// pathsMemo :: (Number, Number) -> Number
// a key from both arguments with a separator, the recursion goes through pathsMemo
const pathsMemo = R.memoizeWith(
  (rows, cols) => rows + ',' + cols,
  (rows, cols) => {
    memoCalls += 1
    if (rows === 0 || cols === 0) return 1
    return pathsMemo(rows - 1, cols) + pathsMemo(rows, cols - 1)
  },
)

console.log(pathsMemo(10, 10))
// each pair (rows, cols) is computed once
console.log('with a cache, computations: ' + memoCalls)

Exercise 2

priceWithTax takes the tax rate in percent from the outside variable taxPercent, so the memoized version returns old prices after the rate changes. Make the function pure: the rate must come in as an argument. Memoize it with R.memoizeWith so that the rate is part of the key. Call priceMemo for the price 100 twice at a 20% rate and once at 10%, print all three prices and then the number of computations, which the calls counter from the starter keeps.

Starter code

// memoize for functions of one argument
// memoize :: (a -> b) -> (a -> b)
const memoize = (fn) => {
  const cache = new Map()
  return (arg) => {
    if (cache.has(arg)) {
      return cache.get(arg)
    }
    const result = fn(arg)
    cache.set(arg, result)
    return result
  }
}

// the counter shows how many times the function actually computed;
// it is a side effect, here only for illustration
let calls = 0

let taxPercent = 20
// priceWithTax :: Number -> Number
const priceWithTax = (price) => {
  calls += 1
  return price + (price * taxPercent) / 100
}
const priceMemo = memoize(priceWithTax)

console.log(priceMemo(100))
taxPercent = 10
// the rate is 10% now, but the price is old
console.log(priceMemo(100))

// your code

Expected output

120
120
110
computations: 2
Show solution
// the counter shows how many times the function actually computed;
// it is a side effect, here only for illustration
let calls = 0
// the rate is an argument now: the result depends only on the arguments, as with a pure function
// priceWithTax :: (Number, Number) -> Number
const priceWithTax = (percent, price) => {
  calls += 1
  return price + (price * percent) / 100
}

// the rate is part of the key: a new rate means a new key
const priceMemo = R.memoizeWith((percent, price) => percent + '|' + price, priceWithTax)

let taxPercent = 20
console.log(priceMemo(taxPercent, 100))
// the same key 20|100: from the cache
console.log(priceMemo(taxPercent, 100))
taxPercent = 10
// the key 10|100: computed again
console.log(priceMemo(taxPercent, 100))
console.log('computations: ' + calls)

Playground

Write and run JavaScript right here — Ramda (R) and Sanctuary (S) are available.

Connected:
playground
Output
—