LRU Cache
vanilla-lru is a zero-dependency JavaScript LRU cache. The exported Lru class inherits from the native Map, preserving the familiar Map interface while adding bounded capacity, LRU promotion, optional expiration, and eviction hooks functionality. You can also use the named createLru() factory to create a new instance.
Install
npm:
Shellnpm install vanilla-lru
script:
HTML<!-- es module --><script type="module"> import { createLru } from 'https://unpkg.com/vanilla-lru/dist/index.js'; const cache = createLru({ maxSize: 1000, maxAge: 1000 * 60 * 5, onEviction(key, value) { console.warn('Evicted:', key, value); }, });</script><!-- umd GlobalName: vanillaLru --><script src="https://unpkg.com/vanilla-lru/dist/index.umd.js"></script>
Compatibility
To maintain compatibility with old projects, two parameter aliases are retained: max and ttl. The mapping is as follows:
max->maxSizettl->maxAge