<!-- Source: https://memory.svelte.page/compare/vs-lru-cache -->
<!-- Canonical: https://memory.svelte.page/compare/vs-lru-cache -->
# Memory Cache vs lru-cache

The Canonical LRU vs a Batteries-Included Cache

## Overview

Compare lru-cache and @humanspeak/memory-cache: the canonical LRU implementation against a TypeScript cache that layers wildcard invalidation, lifecycle hooks, statistics, and a @cached decorator on top of LRU + TTL.

- **Memory Cache site:** https://memory.svelte.page
- **Memory Cache npm:** https://www.npmjs.com/package/%40humanspeak%2Fmemory-cache
- **Memory Cache slug:** memory-cache
- **Category:** LRU Cache
- **Approach:** Highly optimized bounded map with recency tracking, size-aware eviction, and async fetch()
- **GitHub:** https://github.com/isaacs/node-lru-cache
- **npm:** https://www.npmjs.com/package/lru-cache

## Feature comparison

| Feature | @humanspeak/memory-cache | lru-cache | Notes |
| --- | --- | --- | --- |
| Zero Dependencies | Yes | Yes |  |
| TypeScript Support | Written in TypeScript | Written in TypeScript |  |
| LRU Eviction | Yes | Yes | lru-cache is the canonical, heavily tuned implementation. Memory Cache prunes expired entries before evicting any valid least-recently-used key. |
| TTL Expiration | Per cache instance | Per cache + per-entry overrides | lru-cache also supports allowStale and updateAgeOnGet for finer recency/staleness control. |
| Size-Aware Eviction | Entry count + computed weight (maxSize + maxWeight / sizeCalculation) | Entry count + computed size (max + maxSize / sizeCalculation) | Both accept a user-supplied calculator. Memory Cache exposes aggregate weight in getStats(); lru-cache has broader size and disposal tuning. |
| Wildcard / Prefix Deletion | deleteByMagicString + deleteByPrefix | No |  |
| Method Memoization Decorator | @cached, async-aware | memo() method (no decorator) |  |
| Async Fetch De-Duplication | getOrSet() collapses concurrent lookups | fetch() with fetchMethod |  |
| Stale-While-Revalidate | No | allowStale + background fetch |  |
| Lifecycle Instrumentation | Six lifecycle hooks | dispose / onInsert callbacks |  |
| Built-In Statistics | getStats() aggregate counters | Opt-in per-call status tracking |  |
| Browser Support | Yes | Yes |  |

## Memory Cache strengths

- Zero runtime dependencies — works in Node and the browser
- TypeScript-first with fully typed generics (MemoryCache<T>)
- TTL expiration and true LRU eviction in one cache — expired entries are pruned before any valid key is evicted
- Wildcard + prefix bulk invalidation (deleteByMagicString, deleteByPrefix)
- @cached decorator for method-level memoization — async-aware with in-flight de-duplication
- getOrSet() async fetch helper that de-duplicates concurrent lookups for the same key
- Lifecycle hooks (onHit, onMiss, onSet, onDelete, onExpire, onEvict) plus built-in getStats()
- Synchronous reads and writes — no await on the hot path

## lru-cache strengths

- The canonical LRU — extremely optimized and battle-tested at npm scale
- Broader size-calculation, disposal, and tuning controls
- Stale-while-revalidate patterns with allowStale and async fetch()
- Rich low-level controls (peek, dispose, updateAgeOnGet, per-entry TTL)

## Memory Cache limitations

- Smaller community (newer project)
- In-process only — no persistence or multi-process sharing by design
- TTL is configured per cache instance, not per entry

## lru-cache limitations

- Lower-level API with many knobs to hold correctly
- No wildcard or prefix bulk invalidation
- Memoization is a method helper, not a decorator you can put on class methods
- No aggregate hit/miss statistics or lifecycle hook set out of the box

## Verdict

Both libraries can enforce application-defined computed-weight bounds; neither automatically measures retained JavaScript heap. If your bottleneck is raw LRU throughput or you need stale-while-revalidate and extensive low-level tuning, lru-cache remains the reference implementation. Memory Cache adds a @cached decorator, wildcard invalidation, lifecycle hooks, and aggregate stats with a smaller API surface. Pick lru-cache for infrastructure-grade controls; pick Memory Cache when you want application-level caching that reads like TypeScript.

## Keywords

lru-cache alternative, lru-cache vs memory-cache, lru cache typescript, lru cache decorator, javascript cache library, bounded cache node.js
