
Security News
Crates.io Implements Trusted Publishing Support
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
@alloc/quick-lru
Advanced tools
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.
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}"}
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 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.
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
.
$ npm install quick-lru
const QuickLRU = require('quick-lru');
const lru = new QuickLRU({maxSize: 1000});
lru.set('🦄', '🌈');
lru.has('🦄');
//=> true
lru.get('🦄');
//=> '🌈'
Returns a new instance.
Type: object
Required
Type: number
The maximum number of items before evicting the least recently used items.
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.
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
).
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 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 an item.
Check if an item exists.
Get an item without marking it as recently used.
Delete an item.
Returns true
if the item is removed or false
if the item doesn't exist.
Delete all items.
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.
Iterable for all the keys.
Iterable for all the values.
Iterable for all entries, starting with the oldest (ascending in recency).
Iterable for all entries, starting with the newest (descending in recency).
The stored item count.
FAQs
Simple “Least Recently Used” (LRU) cache
The npm package @alloc/quick-lru receives a total of 10,854,444 weekly downloads. As such, @alloc/quick-lru popularity was classified as popular.
We found that @alloc/quick-lru demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.
Research
/Security News
North Korean threat actors deploy 67 malicious npm packages using the newly discovered XORIndex malware loader.