# memoize-lit

> Memoize promise-returning & async functions

Latest version **2.2.0** (published 2026-06-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install memoize-lit
pnpm add memoize-lit
yarn add memoize-lit
bun add memoize-lit
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.2.0 |
| Published | 2026-06-28 |
| First published | 2020-10-31 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 10.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Joel Voss |
| Maintainers | joelvoss |

## Links

- npm: https://www.npmjs.com/package/memoize-lit
- Repository: https://github.com/joelvoss/memoize-lit
- Issues: https://github.com/joelvoss/memoize-lit/issues
- npm.io page: https://npm.io/package/memoize-lit

## Recent versions

- 2.2.0 (latest) — 2026-06-28
- 2.1.1 — 2026-05-09
- 2.1.0 — 2026-04-07
- 2.0.1 — 2026-03-23
- 2.0.0 — 2024-05-23
- 1.6.1 — 2023-10-03
- 1.6.0 — 2022-11-11
- 1.5.0 — 2022-09-05
- 1.4.0 — 2022-07-24
- 1.3.7 — 2022-06-06
- 1.3.6 — 2022-04-20
- 1.3.5 — 2022-02-26
- 1.3.4 — 2021-12-09
- 1.3.3 — 2021-11-28
- 1.3.2 — 2021-11-10
- … 13 more at https://npm.io/package/memoize-lit/versions

## README

# memoize-lit

A memoization library that stores the results of expensive function calls and
returns the cached result when the same inputs occur again.

> Note: This helper works both with synchronous and asynchronous functions,
> e.g. Promises.

This library is a cherry-picked mixture of several popular memoisation
libraries to make memoisation both easy and straight forward.

## Install

```bash
# Using npm
$ npm install memoize-lit

# Using yarn
$ yarn add memoize-lit
```

## Examples

### Synchronous

```js
import { memoize } from 'memoize-lit';

const multiply = (a, b) => a * b;
const memoizedMultiply = memoize(multiply);

memoizedMultiply(2, 3); // 6

memoizedMultiply(2, 3); // 6
// Multiply function is not executed and previous result is returned

memoizedMultiply(1, 3); // 3
// Multiply function is called as arguments have changed

memoizedMultiply(1, 3); // 3
// Multiply function is not executed and previous result is returned

memoizedMultiply(2, 3); // 6
// Multiply function is called as arguments have changed.
```

### Asynchronous

```js
import { memoize } from 'memoize-lit';
import axios from 'axios';

const memoizedAxios = memoize(axios, { maxAge: 100 });

(async () => {
  await memoizedAxios('https://joelvoss.com');

  await memoizedAxios('https://joelvoss.com');
  // Axios is not executed and previous result is returned

  setTimeout(() => {
    await memoizedAxios('https://joelvoss.com');
    // Axios function is called as `maxAge` has expired
  }, 200);
})();
```

## API

### memoize(fn, options?)

Create a memoized version of `fn`. You can provide a custom configuration as
the second argument.

#### fn

Input function to memoize.

```ts
import { memoize } from 'memoize-lit';

// (1) Memoize the `multiply` function
const multiply = (a, b) => a * b;
const memoizedMultiply = memoize(multiply);

// (2) Use it normally
memoizedMultiply(2, 3); // 6
```

#### options

Provide a custom equality function and/or the `maxAge` time in milliseconds.

```ts
// Signature

type EqualityFn = (newArgs: any[], lastArgs: any[]) => boolean;

type Options = {
  isEqual?: EqualityFn;
  maxAge?: number;
};
```

```ts
import { memoize } from 'memoize-lit';
import axios from 'axios';

// (1) Memoize axios
const memoizedAxios = memoize(axios, { maxAge: 100 });

(async () => {
  // (2) Use it normally
  await memoizedAxios('https://joelvoss.com');
})();
```

The default equality function is a shallow equal check of all arguments
(each argument is compared with `===`). If the length of arguments change, then
the default equality function makes no shallow equality checks.

The default `maxAge` value in milliseconds is `2147483647`, which is the
biggest signed 32-bit integer value possible.

## When your function throws or the promise rejects

> In essence: There is no caching if your function throws or is rejected.

If your result function _throws_ then the memoized function will also throw.
This **will not** drop the memoized result. This means that the memoized
function will pretend like it was never called with arguments that made it
throw.

This behaviour is **different** for Promises, because a Promise can never throw
but instead return a rejected state. If a Promise is being rejected, it is being
marked as _stale_ so the next invocation returns a fresh result.

> Note: A rejected promise never overwrites an intermediate successful promise.
> In this case the rejected promise is simply ignored.

This library does not handle promise rejections for you, so always make sure
to wrap an async memoized function in a `try / catch` block.

## Development

[1] Install dependencies

```bash
# Using npm
$ npm install

# Using yarn
$ yarn
```

[2] Validate setup

```bash
$ ./Taskfile.sh validate
```

[3] Start development by running tests in watch-mode

```bash
$ ./Taskfile.sh test -w
```

---

This project was bootstrapped with [@jvdx/core](https://github.com/joelvoss/jvdx-core).

[node+npm]: https://nodejs.org

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