@bigcommerce/memoize

中文文档
1.0.2发布于 2025/05/02MITTypeScriptCommonJS近 30 天下载 8498

A JavaScript library for memoizing the result of a pure function

原始使用文档
文档目录4 节
  1. Install
  2. Usage
  3. Contribution
  4. License

@bigcommerce/memoize

CircleCI

This library can be used to memoize the result of a pure function.

Unlike the default memoize function provided by Lodash, it can be applied to functions that accept multiple non-primitive arguments. It can also be configured to expire its cache after certain number of unique calls. By default, it compares object-based arguments shallowly; but it can be configured to compare arguments strictly or deeply depending on your usage requirement.

Install

You can install this library using npm.

npm install --save @bigcommerce/memoize

Usage

To memoize a function:

function fn(a, b) {
    return { a, b };
}

const memoizedFn = memoize(fn);
const result = memoizedFn({ message: 'hello' }, { message: 'world' });
const result2 = memoizedFn({ message: 'hello' }, { message: 'world' });

expect(result).toBe(result2);

To set a limit on the cache size:

function fn(a, b) {
    return { a, b };
}

const memoizedFn = memoize(fn, { maxSize: 1 });
const result = memoizedFn({ message: 'hello' }, { message: 'world' });

// This call will expire the cache of the previous call because it is called with a different set of arguments
const result2 = memoizedFn({ message: 'hello' }, { message: 'foobar' });
const result3 = memoizedFn({ message: 'hello' }, { message: 'world' });

expect(result3).not.toBe(result);

There is a convenience method for setting the cache size to one:

const memoizedFn = memoizeOne(fn);

To use a different argument comparison function:

const memoizedFn = memoize(fn, { 
    isEqual: (a, b) => a === b,
});

Contribution

To release:

npm run release

To see other available commands:

npm run

License

MIT

版本与发布记录

@bigcommerce/memoize 共发布 3 个版本,首次发布于 2019/08/27;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.2latest2025/05/021.5 年前
1.0.12024/01/182 年前
1.0.02019/08/277 年前

关于 @bigcommerce/memoize 的常见问题

@bigcommerce/memoize 是什么?

A JavaScript library for memoizing the result of a pure function

如何安装 @bigcommerce/memoize?

在项目目录运行 npm install @bigcommerce/memoize,或使用 pnpm add / yarn add / bun add。

@bigcommerce/memoize 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bigcommerce/memoize 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bigcommerce/memoize 可以商用吗?

@bigcommerce/memoize 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bigcommerce/memoize 的下载量有多少?

根据 npm 官方下载统计,@bigcommerce/memoize 在 2026/10/08 – 2026/11/06 这 30 天内被下载 8,498 次,日均约 283 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bigcommerce/memoize 还在维护吗?

最新版本 1.0.2 发布于 2025/05/02(1.5 年前);至今共发布 3 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/08/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。