# @bigcommerce/memoize

> A JavaScript library for memoizing the result of a pure function

Latest version **1.0.2** (published 2025-05-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install @bigcommerce/memoize
pnpm add @bigcommerce/memoize
yarn add @bigcommerce/memoize
bun add @bigcommerce/memoize
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2025-05-01 |
| First published | 2019-08-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 43.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | BigCommerce |
| Maintainers | bcnpmuser, icatalina, chris.boulton, davidchin, pascal.zajac, chanceaclark, jairobc, jmwiese, jorgemoya, cilo, toma-r, leebigcommerce |

## Links

- npm: https://www.npmjs.com/package/@bigcommerce/memoize
- Repository: https://github.com/bigcommerce/memoize-js
- Issues: https://github.com/bigcommerce/memoize-js/issues
- npm.io page: https://npm.io/package/@bigcommerce/memoize

## Dependencies (5)

- [tslib](https://npm.io/package/tslib.md) ^2.8.1
- [shallowequal](https://npm.io/package/shallowequal.md) ^1.1.0
- [lodash.memoize](https://npm.io/package/lodash.memoize.md) ^4.1.2
- [@types/shallowequal](https://npm.io/package/@types/shallowequal.md) ^1.1.5
- [@types/lodash.memoize](https://npm.io/package/@types/lodash.memoize.md) ^4.1.9

## Recent versions

- 1.0.2 (latest) — 2025-05-01
- 1.0.1 — 2024-01-18
- 1.0.0 — 2019-08-27

## README

# @bigcommerce/memoize

[![CircleCI](https://circleci.com/gh/bigcommerce/memoize-js.svg?style=svg)](https://circleci.com/gh/bigcommerce/memoize-js)

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](https://www.npmjs.com/get-npm).

```sh
npm install --save @bigcommerce/memoize
```


## Usage

To memoize a function:

```ts
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:

```ts
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:

```ts
const memoizedFn = memoizeOne(fn);
```

To use a different argument comparison function:

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


## Contribution

To release:

```sh
npm run release
```

To see other available commands:

```sh
npm run
```

## License

MIT

---
_Source: https://npm.io/package/@bigcommerce/memoize · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
