Pacote
    Preparing search index...

    Function memoize

    • Returns a wrapper that caches a function's result for each generated key.

      Repeated calls with the same key return the first result without invoking the wrapped function again. Set capacity to evict least recently used entries when the cache grows beyond the limit.

      Type Parameters

      • A extends unknown[]
      • K
      • V

      Parameters

      • cacheKeyFn: Fn<A, K>

        A function that generates a string key for cached results. This function takes the same arguments as the function to memoize.

      • fn: Fn<A, V>

        The function to memoize.

      • options: Options = {}

        Memoization options.

      Returns MemoizedFn<A, V>

      Version of the function that caches results.

      import { memoize } from '@pacote/memoize'

      const randomFunction = (prefix: string) => `${prefix}${Math.random()}`

      const memoizedFunction = memoize((prefix) => `key_${prefix}`, randomFunction)

      memoizedFunction('foo') // 'foo' followed by randomly-generated number.
      memoizedFunction('foo') // Same result as previous call with 'foo'.

      memoizedFunction('bar') // 'bar' followed by randomly-generated number.
      memoizedFunction('bar') // Same result as previous call with 'bar'.