A high-performance, lightweight LRU (Least Recently Used) cache for JavaScript with O(1) operations and optional TTL support.
Think of an LRU cache like a limited-size bookshelf. When you add a new book and the shelf is full, you remove the least recently used book to make room. Every time you read a book, it moves to the front. This pattern is perfect for caching where you want to keep the most frequently accessed items.
The tiny-lru library provides:
- O(1) operations for get, set, delete, and has
- Optional TTL (Time-To-Live) support for automatic expiration
- Zero dependencies - pure JavaScript
- 100% test coverage - fully tested and reliable
- TypeScript support - full type definitions included
- ~1.6 KB minified and gzipped (compared to ~5.8 KB for lru-cache)
npm install tiny-lruRequires Node.js ≥14 or modern browsers with ES Module support.
import { lru } from "tiny-lru";
// Create a cache that holds up to 100 items
const cache = lru(100);
// Store and retrieve data
cache.set("user:42", {