dynamic-forms / Function

memoize

Generic types:TFunc

Memoizes a function with LRU cache eviction and optional custom cache key resolver.

Presentation

function memoize(
  fn: TFunc,
  resolverOrOptions?:
    | ((...args: Parameters<TFunc>) => string)
    | MemoizeOptions<TFunc>
    | undefined,
): TFunc;

Returns

TFunc -

Memoized function

Parameters

NameTypeDescription
fn
TFunc

Function to memoize

resolverOrOptions
((...args: Parameters<TFunc>) => string) | MemoizeOptions<TFunc> | undefined

Optional key resolver function or options object

Example usage

const expensive = (a: number, b: number) => a + b;
const memoized = memoize(expensive); // Uses default maxSize of 100

// With custom resolver
const withResolver = memoize(expensive, (a, b) => `${a}-${b}`);

// With custom maxSize
const small = memoize(expensive, { maxSize: 10 });