Skip to content
avoidworkPublic

About

A fast, lightweight LRU (Least Recently Used) cache for JavaScript with O(1) operations and optional TTL support.

Resources

Contributing

Stars

184 stars

Watchers

1 watching

Forks

Repository files navigation

Tiny LRU

npm version npm downloads License Node.js version Build Status Coverage

A high-performance, lightweight LRU (Least Recently Used) cache for JavaScript with O(1) operations and optional TTL support.

What is an LRU Cache?

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)

Installation

npm install tiny-lru

Requires Node.js ≥14 or modern browsers with ES Module support.

Quick Start

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", {