Function memoize

  • Creates a function that memoizes the result of func. If resolver is provided, it determines the cache key for storing the result based on the arguments provided to the memoized function. By default, the first argument provided to the memoized function is used as the map cache key. The func is invoked with the this binding of the memoized function.

    Note: The cache is exposed as the cache property on the memoized function. Its creation may be customized by replacing the memoize.Cache constructor with one whose instances implement the Map method interface of clear, delete, get, has, and set.




    const object = { 'a': 1, 'b': 2 }
    const other = { 'c': 3, 'd': 4 }

    const values = memoize(values)
    // => [1, 2]

    // => [3, 4]

    object.a = 2
    // => [1, 2]

    // Modify the result cache.
    values.cache.set(object, ['a', 'b'])
    // => ['a', 'b']

    // Replace `memoize.Cache` constructor implementation.
    memoize.Cache = WeakMap

    Type Parameters

    • T extends ((...args) => any)

    • K


    • func: T

      The function to have its output memoized.

    • Optional resolver: ((...args) => K)

      The function to resolve the cache key. default will use the FIRST argument as key

        • (...args): K
        • Parameters

          • Rest ...args: Parameters<T>

          Returns K

    Returns MemorizedFunction<T, K, ReturnType<T>>

    Returns the new memoized function.