What is @alloc/quick-lru?
The @alloc/quick-lru package is a fast, lightweight least recently used (LRU) cache implementation. It is designed to efficiently store and retrieve items based on their usage, ensuring that the most recently accessed items are kept in memory while older items are discarded when the cache reaches its capacity. This package is useful for optimizing performance in applications where accessing data from memory is preferable to repeatedly fetching it from slower sources like databases or file systems.
What are @alloc/quick-lru's main functionalities?
Creating and using an LRU cache
This feature demonstrates how to create an LRU cache with a maximum size, add an item to the cache, retrieve an item, check if an item exists in the cache, and delete an item from the cache.
{"const QuickLRU = require('@alloc/quick-lru');\nconst lru = new QuickLRU({ maxSize: 100 });\nlru.set('key', 'value');\nconsole.log(lru.get('key')); // 'value'\nconsole.log(lru.has('key')); // true\nlru.delete('key');\nconsole.log(lru.has('key')); // false"}
Iterating over cache items
This feature shows how to iterate over the items in the cache. It's useful for scenarios where you need to perform operations on all items stored in the cache.
{"const QuickLRU = require('@alloc/quick-lru');\nconst lru = new QuickLRU({ maxSize: 2 });\nlru.set('a', 1);\nlru.set('b', 2);\nfor (const [key, value] of lru) {\n console.log(key, value);\n}"}
Other packages similar to @alloc/quick-lru
lru-cache
lru-cache is another popular LRU cache implementation. It offers a rich set of features like item expiration, cache resizing, and a more extensive API for cache manipulation. Compared to @alloc/quick-lru, it might be more suitable for applications requiring these advanced features, but it could also be heavier in terms of performance and resource usage.
tiny-lru
tiny-lru is a minimalistic LRU cache implementation focused on performance and a small footprint. It provides basic LRU cache functionality similar to @alloc/quick-lru but with fewer features. It's an excellent choice for projects where simplicity and efficiency are paramount.
quick-lru
Simple “Least Recently Used” (LRU) cache
Useful when you need to cache something and limit memory usage.
Inspired by the hashlru
algorithm, but instead uses Map
to support keys of any type, not just strings, and values can be undefined
.
Install
$ npm install quick-lru
Usage
const QuickLRU = require('quick-lru');
const lru = new QuickLRU({maxSize: 1000});
lru.set('🦄', '🌈');
lru.has('🦄');
lru.get('🦄');
API
new QuickLRU(options?)
Returns a new instance.
options
Type: object
maxSize
Required
Type: number
The maximum number of items before evicting the least recently used items.
maxAge
Type: number
Default: Infinity
The maximum number of milliseconds an item should remain in cache.
By default maxAge will be Infinity, which means that items will never expire.
Lazy expiration happens upon the next write
or read
call.
Individual expiration of an item can be specified by the set(key, value, options)
method.
onEviction
Optional
Type: (key, value) => void
Called right before an item is evicted from the cache.
Useful for side effects or for items like object URLs that need explicit cleanup (revokeObjectURL
).
Instance
The instance is iterable
so you can use it directly in a for…of
loop.
Both key
and value
can be of any type.
.set(key, value, options?)
Set an item. Returns the instance.
Individual expiration of an item can be specified with the maxAge
option. If not specified, the global maxAge
value will be used in case it is specified on the constructor, otherwise the item will never expire.
.get(key)
Get an item.
.has(key)
Check if an item exists.
.peek(key)
Get an item without marking it as recently used.
.delete(key)
Delete an item.
Returns true
if the item is removed or false
if the item doesn't exist.
.clear()
Delete all items.
.resize(maxSize)
Update the maxSize
, discarding items as necessary. Insertion order is mostly preserved, though this is not a strong guarantee.
Useful for on-the-fly tuning of cache sizes in live systems.
.keys()
Iterable for all the keys.
.values()
Iterable for all the values.
.entriesAscending()
Iterable for all entries, starting with the oldest (ascending in recency).
.entriesDescending()
Iterable for all entries, starting with the newest (descending in recency).
.size
The stored item count.