Skip to main content
cngx-src documentation

memoize

Functioncore/utilsv0.1.0

projects/core/utils/memo.util.ts

Description#

Creates a memoized version of a single-argument pure function. Each call to memoize() produces an independent cache.

const expensive = memoize((id: string) => computeHeavy(id)); expensive('a'); // computes expensive('a'); // cached

The default cache is unbounded and holds strong references to every key and value for the lifetime of the memoized closure. That is only safe when the key space is provably finite (locales, enum keys, a fixed config set). Any call site that derives keys from unbounded input - user text, entity ids, dates - must set MemoizeOptions.cacheLimit, otherwise the cache is a leak by design.

Signature#

memoize(fn, options?: MemoizeOptions)

Parameters#

@paramfn
@paramoptions?MemoizeOptions

Returns#

V