Registry / storage / map-lru

map-lru

JSON →
library2.1.0jsnpmunverified

An LRU cache implementation that is API-compatible with ES6 Map, allowing drop-in replacement for caching with limited memory usage. Version 2.1.0 is current, with stable releases via npm. It supports Node.js >=12 and ships TypeScript type definitions. Unlike many LRU caches, MapLRU mimics the Map interface exactly, enabling use of standard methods like set, get, has, delete, clear, keys, values, entries, and forEach. It also adds extra methods: peek (retrieve without marking as used), last (last accessed key), and keysAccessed (ordered keys). It is lightweight, has no dependencies, and is licensed under Unlicense.

npm install map-lru
INSTALL
IMPORT
SIG · MAP-LRU
M
map-lru
storagejavascriptv2.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

MapLRU
import MapLRU from 'map-lru'
const MapLRU = require('map-lru').default
ESM default export. CJS require works directly: const MapLRU = require('map-lru'). TypeScript types included.
MapLRU
const MapLRU = require('map-lru')
const MapLRU = require('map-lru').MapLRU
CJS: the default export is the class itself, so require returns the constructor directly.
MapLRU
import type { MapLRU } from 'map-lru'
import { MapLRU } from 'map-lru'
When only using types, use import type to avoid bundling the module. The class is the default export, not a named export.

Demonstrates basic LRU cache operations: instantiation, set/get, automatic eviction, last accessed key, peek, delete, clear.

import MapLRU from 'map-lru'; const cache = new MapLRU(3); // max 3 entries cache.set('a', 1); cache.set('b', 2); cache.set('c', 3); console.log(cache.get('a')); // 1, 'a' becomes most recent cache.set('d', 4); // evicts 'b' (least recently used) console.log(cache.has('b')); // false console.log(cache.size); // 3 console.log(cache.last); // 'd' (last accessed key) console.log([...cache.keys()]); // ['a', 'c', 'd'] (access order) console.log(cache.peek('c')); // 3, does not update recency cache.delete('a'); console.log(cache.size); // 2 cache.clear(); console.log(cache.size); // 0
Debug
Known issues
gotchaMapLRU constructor requires a maxSize (number) argument. Calling without it or with non-number may cause unexpected behavior (e.g., no caching).
fix
Always pass a positive integer to new MapLRU(maxSize).
affects: >=0.0.0
gotchaThe keys() method returns keys in insertion order (like Map), not access order. Use keysAccessed() for access order.
fix
Use cache.keysAccessed() to iterate keys in order of most recently used (last is the most recent).
affects: >=0.0.0
gotchaThe `last` property returns the key of the last accessed entry, but if the cache is empty, it returns undefined.
fix
Check cache.size > 0 before accessing cache.last.
affects: >=0.0.0
breakingVersion 2.0.0 changed export from CommonJS to default ESM export. Older require() patterns may break if not updated.
fix
Use import MapLRU from 'map-lru' for ESM, or const MapLRU = require('map-lru') for CJS.
affects: >=2.0.0
Errors
Common errors & fixes
MapLRU is not a constructor
Using require('map-lru').default in CJS; the default export is the constructor itself, not wrapped in an object.
fix
const MapLRU = require('map-lru');
Cannot read property 'set' of undefined
Attempting to call methods on a map-lru instance that was not properly constructed (e.g., using default import without new).
fix
Ensure you use new MapLRU(maxSize) after import.
MapLRU is not iterable
Trying to use spread operator on MapLRU directly; it is not iterable by default. Use entries() or keys().
fix
Use [...cache.entries()] or [...cache.keys()].
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
16
OpenAI (training)
1
Resources
map-lru — npm install map-lru · libregistry