# single-user-cache

> A cache for a single user

Latest version **2.1.0** (published 2025-09-25) · MIT license · 0 weekly downloads

## Install

```sh
npm install single-user-cache
pnpm add single-user-cache
yarn add single-user-cache
bun add single-user-cache
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

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

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2025-09-25 |
| First published | 2019-03-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 22.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Matteo Collina |
| Maintainers | matteo.collina |
| Keywords | cache, single, user, graphql |

## Links

- npm: https://www.npmjs.com/package/single-user-cache
- Repository: https://github.com/mcollina/single-user-cache
- Homepage: https://github.com/mcollina/single-user-cache#readme
- Issues: https://github.com/mcollina/single-user-cache/issues
- npm.io page: https://npm.io/package/single-user-cache

## Dependencies (1)

- [safe-stable-stringify](https://npm.io/package/safe-stable-stringify.md) ^2.4.3

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2025-09-25
- 2.0.0 — 2025-05-13
- 1.0.1 — 2023-11-16
- 1.0.0 — 2023-11-15
- 0.6.0 — 2021-10-24
- 0.5.0 — 2021-07-13
- 0.4.0 — 2021-02-18
- 0.3.1 — 2020-08-07
- 0.3.0 — 2019-04-19
- 0.2.0 — 2019-04-18
- 0.1.0 — 2019-03-08

## README

# single-user-cache

A simple cache system for a single user request, built on the same concepts of [data loader](https://github.com/facebook/dataloader).

## Install

```js
npm i single-user-cache
```

## Usage

```js
const { Factory } = require('.')
const factory = new Factory()

factory.add('fetchSomething', {
  // cache by default, set to false to just do batching
  cache: true,
  // unlimited batch size by default, set to a number > 0 to split the batches in chunks
  maxBatchSize: undefined,
}, async (queries, context) => {
  console.log(queries)
  // [ 42, 24 ]

  console.log(context)
  // { some: 'data' }

  return queries.map((k) => {
    return { k }
  })
})

async function run () {
  const context = {
    some: 'data'
  }
  const cache = factory.create(context)

  const p1 = cache.fetchSomething(42)
  const p2 = cache.fetchSomething(24)

  const res = await Promise.all([p1, p2])

  console.log(res)
  // [
  //   { k: 42 },
  //   { k: 24 }
  // ]
}

run().catch(console.log)
```

If the query parameter is an object, its cache key will be generated
using
[safe-stable-stringify](https://github.com/BridgeAR/safe-stable-stringify).

## Benchmarks

Compared to [dataloader](http://npm.im/dataloader), this library is significantly
faster.

```
➜  node benchmark.js
clk: ~4.21 GHz
cpu: Apple M4 Max
runtime: node 22.15.0 (arm64-darwin)

benchmark                   avg (min … max) p75 / p99    (min … top 1%)
------------------------------------------- -------------------------------
dataloader                     1.09 ms/iter   1.30 ms  █
                      (718.92 µs … 2.04 ms)   1.72 ms  █      ▄▂
                    (  2.81 mb …   6.82 mb)   6.73 mb ██▆▂▁▁▂███▇▆▇▅▄▆▂▃▃▃▁

single-user-cache            345.48 µs/iter 337.71 µs  ▄▇█
                    (297.83 µs … 601.96 µs) 526.33 µs ████▃
                    (  1.20 mb …   2.08 mb)   1.85 mb █████▃▁▁▁▁▁▁▁▁▁▁▂▄▅▅▂
```

## License

MIT

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