least-recent
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

least-recent

Provides an implementation of LRUCache. Effectively a fork of LRUCache from Mnemonist with one additional feature.

LRU standing for least recently used, can be seen as a a fixed-capacity key-value store that will evict infrequent items when full and setting new keys.

For instance, if one creates a LRUCache with a capacity of 1000 and one inserts a thousand-and-first key, the cache will forget its least recently used key-value pair in order not to overflow the allocated memory.

This structure is very useful to cache the result of costly operations when one cannot afford to keep every result in memory and only want to keep the most frequent ones.

For more information, you can check this Wikipedia page.

import { LRUCache } from "least-recent";

Usage

The LRUCache takes a single argument to create: the desired capacity. You could also provide types for keys and values.

const cache = new LRUCache(1000);
const stringCache = new LRUCache<string, string>(1000);

For available methods, please see Mnemonist LRUCache documentation

The only difference from Mnemonist, is we emit an event (using nanoevents) if an entry gets evicted.

import { LRUCache } from "least-recent";
const cache = new LRUCache(1000);
cache.on("evicted", (key, value) => {
  console.log("evicted entry", key, value);
});

Readme

Keywords

Package Sidebar

Install

npm i least-recent

Weekly Downloads

1,536

Version

1.0.3

License

(MIT OR Apache-2.0)

Unpacked Size

33 kB

Total Files

10

Last publish

Collaborators

  • ukstv