<A extends object, O>(f: (a: A) => O): (ast: A) => OCreates a memoized function whose input is an object, caching results by object identity.
When to use
Use to reuse the result of a synchronous computation whose output is stable for a given object reference.
Details
Each memoized wrapper owns a private WeakMap keyed by object identity.
Cached undefined results are still returned because the cache is checked
with WeakMap.has.
Gotchas
Structurally equal objects do not share cache entries. If the same object is mutated after its first call, later calls still return the cached result for that reference.
export function function memoize<A extends object, O>(
f: (a: A) => O
): (ast: A) => O
Creates a memoized function whose input is an object, caching results by
object identity.
When to use
Use to reuse the result of a synchronous computation whose output is stable
for a given object reference.
Details
Each memoized wrapper owns a private WeakMap keyed by object identity.
Cached undefined results are still returned because the cache is checked
with WeakMap.has.
Gotchas
Structurally equal objects do not share cache entries. If the same object is
mutated after its first call, later calls still return the cached result for
that reference.
memoize<function (type parameter) A in memoize<A extends object, O>(f: (a: A) => O): (ast: A) => OA extends object, function (type parameter) O in memoize<A extends object, O>(f: (a: A) => O): (ast: A) => OO>(f: (a: A) => Of: (a: A extends objecta: function (type parameter) A in memoize<A extends object, O>(f: (a: A) => O): (ast: A) => OA) => function (type parameter) O in memoize<A extends object, O>(f: (a: A) => O): (ast: A) => OO): (ast: A extends objectast: function (type parameter) A in memoize<A extends object, O>(f: (a: A) => O): (ast: A) => OA) => function (type parameter) O in memoize<A extends object, O>(f: (a: A) => O): (ast: A) => OO {
const const cache: WeakMap<object, O>cache = new var WeakMap: WeakMapConstructor
new <object, O>(entries?: readonly (readonly [object, O])[] | null | undefined) => WeakMap<object, O> (+1 overload)
WeakMap<object, function (type parameter) O in memoize<A extends object, O>(f: (a: A) => O): (ast: A) => OO>()
return (a: A extends objecta) => {
if (const cache: WeakMap<object, O>cache.WeakMap<object, O>.has(key: object): booleanhas(a: A extends objecta)) {
return const cache: WeakMap<object, O>cache.WeakMap<object, O>.get(key: object): O | undefinedget(a: A extends objecta)!
}
const const result: Oresult = f: (a: A) => Of(a: A extends objecta)
const cache: WeakMap<object, O>cache.WeakMap<object, O>.set(key: object, value: O): WeakMap<object, O>Adds a new element with a specified key and value.
set(a: A extends objecta, const result: Oresult)
return const result: Oresult
}
}