A least-frequently-used (LFU) cache for Node.js, directly ported from lru-cache but using an LFU eviction algorithm based on the paper by Dhruv Bird. Current version 5.0.0 provides a configurable cache with options for max size, item length calculation, maxAge, dispose callbacks, stale returns, and noDisposeOnSet. The API includes set, get, peek, del, reset, has, and forEach. Unlike LRU caches, which evict least-recently-used items, this cache evicts least-frequently-used items, making it suitable for workloads where access frequency matters more than recency.
npm install node-lfu-cacheNo compatibility data collected yet for this library.
Verified import paths — ran on the pinned version, not inferred.
Basic setup and usage of node-lfu-cache: constructor with options, set, get, del, and reset.
Use import syntax: import LFU from 'node-lfu-cache'. If you need CJS, stay on 4.x.
Use new LFU(options) instead of LFU(options).
Update your length function to (value, key) => ... . In v5, the order is (value, key) as well.
Check item length before setting if you rely on dispose for cleanup.
Wrap any cache operations inside dispose with setImmediate or process.nextTick.
For v5: import LFU from 'node-lfu-cache'; new LFU(options). For older versions: const LFU = require('node-lfu-cache'); new LFU(options).Run npm install node-lfu-cache --save. If importing from a subpath, use import LFU from 'node-lfu-cache' (not 'node-lfu-cache/src').
Use import LFU from 'node-lfu-cache' or downgrade to v4.x with const LFU = require('node-lfu-cache').Ensure options.length is a function: length: (value, key) => value.length (or similar).