Memoize a Function

Write the code, see it run, pass the checks.

The problem

  • medium
  • Code

Write memoize(fn, resolver): return a function that calls fn with all its arguments and caches the result under a key, so a later call with the same key returns the cached result without calling fn. The key is resolver's return value for the same arguments when a resolver is given, and otherwise the first argument alone. Compare keys the way a Map does: 1 and the string 1 are different keys, and two objects are the same key only if they're the same object. Cache every result, undefined included.

Your code must pass

  • Calls fn once per key and returns the cached result
  • Tells keys apart by type and by object identity
  • Uses the resolver's result as the key, or else the first argument
  • Caches a result of undefined too