
DataLoader
A small JavaScript helper that batches and caches data requests to cut down on repeated calls to databases or
- Stars
- 13.4k
- Forks
- 522
- Licence
- MIT
- Language
- JavaScript
- Last update
- 19 days ago
- Type
- Library
What it is
DataLoader is a utility for Node.js apps that sits in the data-fetching layer and gives your code one simple way to load individual items. Behind the scenes it groups many individual requests made in the same tick of the event loop into one batch call to your backend, and caches results within a single request. It is most often used with GraphQL servers built on graphql-js.
- Batch user lookups so that many resolvers requesting users by ID trigger a single database query
- Reduce duplicate loads of the same record within one web request by relying on the built-in memoization
- Use a custom batchScheduleFn to control when batches are dispatched, such as collecting requests over a fixed time window
Great for
- Node.js developers building a GraphQL server with graphql-js
- Services that load many related records (such as users or rows by ID) and want fewer round-trips to a database
- Teams who want a per-request cache for repeated lookups without adding a shared cache like Redis
Not ideal for
- Projects that need a shared, cross-request cache; the README says DataLoader's cache is per-request and does not replace Redis or Memcache
- Non-JavaScript stacks, since this package is for JavaScript and Node.js services (ports to other languages are only encouraged, not provided here)
- Simple apps making one or two data calls, where batching adds little benefit
How to get it
- 1
Install with a package manager
You'll need: Node.js (README notes global ES6 Promise and Map support is required; exact version not stated in the excerpt)
Install with npm
npm install --save dataloader - 2
Run it
- Install the package with npm install --save dataloader.
- Create a loader with new DataLoader(batchFn), where batchFn takes an array of keys and returns a Promise of an array of values.
- Call loader.load(key) wherever you need a single item; calls in the same tick are batched.
- Create a new loader per request in a web server so cached data is not shared between users.
- 3
Get the most out of it
- Return an Error instance in the results array for missing keys so individual loads reject cleanly, per the batch function rules in the README.
- Keep results aligned with the input key order, since the README requires output arrays to match key indexes.
- Use batchScheduleFn to customize batching timing, such as a setTimeout window or manual dispatch, understanding the latency trade-off.
- Use it inside a graphql-js service, where the README says it is most often used.
Downloads
Strengths
- Simple, generic API: you write one batch function and call load() for single items
- Batching and per-request caching happen automatically, without changing calling code
- Long-standing, MIT-licensed project with a documented design and a clear README
Trade-offs
- Caching is in-memory and per-instance, so you must create loaders per request to avoid leaking data between users
- Custom schedulers that wait (e.g. a 100ms timer) add latency, as the README notes
How it compares
| Project | Stars | Type | Level | Updated | Best for |
|---|---|---|---|---|---|
| dataloader (this) | 13.4k | Library | intermediate | 19 days ago | Node.js developers building a GraphQL server with graphql-js |
| gqlgen | 10.8k | Library | intermediate | 8 days ago | Go developers building a new GraphQL API |
| query | 50.5k | Library | intermediate | 4 days ago | Frontend developers building apps that load data from a server |
| graphql-js | 20.3k | Library | intermediate | 4 days ago | JavaScript or TypeScript developers building a GraphQL API on Node.js |
| apollo-client | 19.8k | Library | intermediate | 5 days ago | Developers building React apps that query a GraphQL API |
| apollo-server | 14k | Library | intermediate | 5 days ago | Node.js developers who want to add a GraphQL API to an existing Express, Koa or Hapi app |
Stats synced from GitHub. The assessment above was drafted with AI from the project's README on 2026-10-29 and may miss recent changes — check the README before relying on a command. MIT licence: free to use, modify and distribute, including commercially, as long as you keep the copyright notice.