Function memoize

Memoizes a function, caching the result of the last call. If the function is called with the same arguments (and the same context!) again, it will return the cached result.

It caches only the last call, so it's not suitable for functions that are called with different arguments in a short time.

Warning: Your function must be 100% pure, and it can't access anything dynamic outside its scope.

  • Type Parameters

    • Args extends unknown[]
    • Ret

    Parameters

    • fn: ((...args: Args) => Ret)

      function to memoize

        • (...args): Ret
        • Parameters

          Returns Ret

    Returns ((this: unknown, ...args: Args) => Ret)

    memoized function

      • (this, ...args): Ret
      • Parameters

        • this: unknown
        • Rest...args: Args

        Returns Ret