Registry / storage / nanolru

nanolru

JSON →
library1.0.0jsnpmunverified

A simple O(1) LRU cache for Node.js, version 1.0.0 (stable). Forked from 'lru' to remove the dependency on 'events', providing a minimal, zero-dependency implementation. Supports set, get, peek, remove, and clear operations with configurable max size and optional max age for automatic eviction. Suitable for scenarios requiring a lightweight, fast LRU cache without external dependencies.

npm install nanolru
INSTALL
IMPORT
SIG · NANOLRU
N
nanolru
storagejavascriptv1.0.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.

LRU
const LRU = require('nanolru')
import LRU from 'nanolru'
CommonJS module. No ES module export, so import syntax does not work.
LRU
const LRU = require('nanolru')
var LRU = require('nanolru').LRU
Default export is the constructor; no named export.
LRU (as factory)
const cache = new LRU(100)
const cache = new LRU({ max: 100 })
Constructor accepts max length as a number, or an options object with 'max' and 'maxAge'.

Creates an LRU cache with max size 2, demonstrates set/get, eviction, remove, keys, and clear.

const LRU = require('nanolru'); const cache = new LRU(2); cache.set('foo', 'bar'); console.log(cache.get('foo')); // => 'bar' cache.set('foo2', 'bar2'); cache.set('foo3', 'bar3'); // evicts 'foo' console.log(cache.get('foo3')); // => 'bar3' console.log(cache.length); // => 1 cache.remove('foo2'); console.log(cache.keys); // => ['foo3'] cache.clear(); console.log(cache.length); // => 0
Debug
Known issues
gotchaThe constructor expects the first argument as max length (number) or options object. Passing a single number works; passing string or undefined may cause unexpected behavior.
fix
Always pass a number or { max: number, maxAge?: number } to the constructor.
affects: >=1.0.0
gotchaThe 'keys' property returns an array of keys, but it is not documented as read-only; mutating it might affect internal state.
fix
Treat 'cache.keys' as read-only. Do not push, splice, or reassign.
affects: >=1.0.0
gotchaPeek method does not update the recentness of the key, but it still returns the value if present. Ensure you intend to not mark the key as recently used.
fix
Use cache.get(key) to mark as recently used, or cache.peek(key) to check without updating order.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: LRU is not a constructor
Using ES import syntax (import LRU from 'nanolru') which does not work in CommonJS module.
fix
Use const LRU = require('nanolru')
TypeError: cache.set is not a function
Cache instance created incorrectly, e.g., calling LRU() without new or passing invalid options.
fix
Ensure you use new LRU(max) or new LRU({ max: max })
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
16
Amazon
1
Resources
nanolru — npm install nanolru · libregistry