Type Alias MemoizeOptions<Fn, Key, Cache>Experimental

MemoizeOptions<Fn, Key, Cache>: {
    cache?: Cache;
    errorIsCacheable?: ((err: unknown) => boolean);
    getKey?: ((this: ThisParameterType<Fn>, ...args: Parameters<Fn>) => Key);
}

Options for memoize.

UNSTABLE: New API, yet to be vetted.

Type Parameters

Type declaration

  • Optional Experimentalcache?: Cache

    Provide a custom cache for getting previous results. By default, a new Map object is instantiated upon memoization and used as a cache, with no limit on the number of results to be cached.

    Alternatively, you can supply a LruCache with a specified max size to limit memory usage.

  • OptionalerrorIsCacheable?: ((err: unknown) => boolean)

    Callback to determine if an error or other thrown value is cacheable.

    {() => false}
    
      • (err): boolean
      • Parameters

        • err: unknown

          The thrown error or other value.

        Returns boolean

        true if the error is cacheable, false otherwise.

  • OptionalgetKey?: ((this: ThisParameterType<Fn>, ...args: Parameters<Fn>) => Key)

    Function to get a unique cache key from the function's arguments. By default, a composite key is created from all the arguments plus the this value, using reference equality to check for equivalence.

    import { memoize } from "@std/cache";
    import { assertEquals } from "@std/assert";

    const fn = memoize(({ value }: { cacheKey: number; value: number }) => {
    return value;
    }, { getKey: ({ cacheKey }) => cacheKey });

    assertEquals(fn({ cacheKey: 1, value: 2 }), 2);
    assertEquals(fn({ cacheKey: 1, value: 99 }), 2);
    assertEquals(fn({ cacheKey: 2, value: 99 }), 99);
      • (this, ...args): Key
      • Parameters

        • this: ThisParameterType<Fn>
        • Rest...args: Parameters<Fn>

        Returns Key