Skip to content

Function: memoizeWeak() ​

ts
function memoizeWeak<TKey, TResult>(fn): (key) => TResult;

Memoizes a function using a WeakMap keyed by object identity. 使用基于对象标识的 WeakMap 为函数做记忆化。

Type Parameters ​

Type ParameterDescription
TKey extends objectThe object key type / 对象键类型
TResultThe return type / 返回值类型

Parameters ​

ParameterTypeDescription
fn(key) => TResultThe target function / 目标函数

Returns ​

A memoized function / 记忆化后的函数

(key) => TResult

Example ​

ts
const fn = memoizeWeak((obj) => Object.keys(obj).length)
fn({})
// => 0

Since ​

1.2.0

Released under the MIT License.