lru.min

NPM Versionarrow-up-right NPM Downloadsarrow-up-right Coveragearrow-up-right GitHub Workflow Status (Node.js)arrow-up-right GitHub Workflow Status (Bun)arrow-up-right GitHub Workflow Status (Deno)arrow-up-right

🔥 An extremely fast, efficient, and lightweight LRUarrow-up-right Cache for JavaScript (Browser compatible).

Why another LRU?

  • 🎖️ lru.min is fully compatible with both Node.js (8+), Bun, Deno and, browser environments. All of this, while maintaining the same high performance (and a little more)arrow-up-right as the most popular LRU packages.


Install

# Node.js
npm i lru.min
# Bun
bun add lru.min
# Deno
deno add npm:lru.min

Usage

Quickstart

For up-to-date documentation, always follow the README.mdarrow-up-right in the GitHub repository.

Import

ES Modules

CommonJS

Browser

Requires ES6.

Create a new LRU Cache

Set maximum size when creating LRU.

Also, you can set a callback for every deletion/eviction:

Set a cache

Adds a key-value pair to the cache. Updates the value if the key already exists

undefined keys will simply be ignored.

  • Complexity: O(1).

Get a cache

Retrieves the value for a given key and moves the key to the most recent position.

  • Complexity: O(1).

Peek a cache

Retrieves the value for a given key without changing its position.

  • Complexity: O(1).

Check if a key exists

  • Complexity: O(1).

Delete a cache

  • Complexity: O(1).

Evict from the oldest cache

Evicts the specified number of the oldest items from the cache.

  • Complexity: O(key) — even if passed a number greater than the number of items, only existing items will be evicted.

[!TIP]

  • Methods that perform eviction(s) when maximum size is reached: set and resize.

  • Methods that always perform eviction(s): delete, clear, and evict itself.

Resize the cache

Resizes the cache to a new maximum size, evicting items if necessary.

  • Complexity:

    • Increasing: O(newMax - max).

    • Downsizing: O(n).

Clear the cache

Clears and disposes (if used) all key-value pairs from the cache.

  • Complexity:

    • Without onEviction: O(1).

    • Using onEviction: O(entries).

Debugging

Get the max size of the cache

  • Complexity: O(1).

Get the current size of the cache

  • Complexity: O(1).

Get the available slots in the cache

  • Complexity: O(1).

Iterating the cache

Get all keys

Iterates over all keys in the cache, from most recent to least recent.

  • Complexity: O(keys).

Get all values

Iterates over all values in the cache, from most recent to least recent.

  • Complexity: O(values).

Get all entries

Iterates over [key, value] pairs in the cache, from most recent to least recent.

  • Complexity: O(entries).

Run a callback for each entry

Iterates over each value-key pair in the cache, from most recent to least recent.

  • Complexity: O(entries).


[!NOTE]

  • We use O(keys), O(values), O(entries), and O(newMax - max) to explicitly indicate what is being iterated over. In traditional complexity notation, this would be represented as O(n).


TypeScript

You can set types for both keys and values. For example:

Also:


Performance

The benchmark is performed by comparing 1,000,000 runs through a maximum cache limit of 100,000, getting 333,333 caches and deleting 200,000 keys 10 consecutive times, clearing the cache every run.


Security Policy

GitHub Workflow Status (with event)arrow-up-right

Please check the SECURITY.mdarrow-up-right.


Contributing

See the Contributing Guidearrow-up-right and please follow our Code of Conductarrow-up-right 🚀


Acknowledgements

lru.min is based and inspired on the architecture and code of both lru-cachearrow-up-right and quick-lruarrow-up-right, simplifying their core concepts for enhanced performance and compatibility.

For more comprehensive features such as TTL support, consider using and supporting them 🤝


What comes from lru-cachearrow-up-right?

Architecture's essence:

It's not the same code, but majority based on thisarrow-up-right.


What comes from quick-lruarrow-up-right?

Name of methods and options (including their final functionality ideas):

  • resize

  • peek

  • onEviction

  • forEach

  • entriesDescending as entries


License

lru.min is under the MIT Licensearrow-up-right. Copyright © 2024-present Weslley Araújoarrow-up-right and lru.min contributorsarrow-up-right.

Last updated