What is tiny-lru?
The tiny-lru npm package is a lightweight, in-memory Least Recently Used (LRU) cache. It is designed to be simple and efficient, making it suitable for use in environments where memory usage and performance are critical.
What are tiny-lru's main functionalities?
Creating an LRU Cache
This feature allows you to create an LRU cache with a specified maximum size. The cache will automatically evict the least recently used items when the size limit is reached.
const LRU = require('tiny-lru');
const cache = LRU(100); // Create a cache with a max size of 100 items
Setting and Getting Cache Items
You can store items in the cache using the `set` method and retrieve them using the `get` method. If the item is not found, `get` will return `undefined`.
cache.set('key', 'value');
const value = cache.get('key'); // 'value'
Deleting Cache Items
This feature allows you to delete specific items from the cache using the `delete` method.
cache.set('key', 'value');
cache.delete('key');
const value = cache.get('key'); // undefined
Clearing the Cache
You can clear all items from the cache using the `clear` method, which removes all entries.
cache.set('key1', 'value1');
cache.set('key2', 'value2');
cache.clear();
const value1 = cache.get('key1'); // undefined
const value2 = cache.get('key2'); // undefined
Cache Size and Item Count
This feature allows you to check the current number of items in the cache using the `size` property.
cache.set('key1', 'value1');
cache.set('key2', 'value2');
const size = cache.size; // 2
Other packages similar to tiny-lru
lru-cache
The lru-cache package is another popular LRU cache implementation for Node.js. It offers more features and configuration options compared to tiny-lru, such as time-to-live (TTL) settings for cache entries and more detailed cache statistics.
quick-lru
The quick-lru package is a minimalist LRU cache implementation that focuses on performance and simplicity. It is similar to tiny-lru in terms of its lightweight nature but offers a slightly different API.
node-cache
The node-cache package provides a simple and efficient in-memory caching solution with support for TTL and other advanced features. It is more feature-rich compared to tiny-lru, making it suitable for more complex caching needs.
Tiny LRU
A lightweight, high-performance Least Recently Used (LRU) cache implementation for JavaScript with optional TTL (time-to-live) support. Works in both Node.js and browser environments.
Installation
npm install tiny-lru
Usage
Factory Function
import {lru} from "tiny-lru";
const cache = lru(max, ttl = 0, resetTtl = false);
Class Constructor
import {LRU} from "tiny-lru";
const cache = new LRU(1000, 60000, true);
const cache2 = new LRU(100, 5000);
cache2.set('key1', 'value1');
Class Inheritance
import {LRU} from "tiny-lru";
class MyCache extends LRU {
constructor(max, ttl, resetTtl) {
super(max, ttl, resetTtl);
}
}
Parameters
- max
{Number}
- Maximum number of items to store. 0 means unlimited (default: 1000)
- ttl
{Number}
- Time-to-live in milliseconds, 0 disables expiration (default: 0)
- resetTtl
{Boolean}
- Reset TTL on each set()
operation (default: false)
Parameter Validation
The factory function validates parameters and throws TypeError
for invalid values:
try {
const cache = lru(-1);
} catch (error) {
console.error(error.message);
}
try {
const cache = lru(100, -1);
} catch (error) {
console.error(error.message);
}
try {
const cache = lru(100, 0, "true");
} catch (error) {
console.error(error.message);
}
Interoperability
Compatible with Lodash's memoize
function cache interface:
import _ from "lodash";
import {lru} from "tiny-lru";
_.memoize.Cache = lru().constructor;
const memoized = _.memoize(myFunc);
memoized.cache.max = 10;
Testing
Tiny-LRU maintains 100% code coverage:
--------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
--------------|---------|----------|---------|---------|-------------------
All files | 100 | 96.34 | 100 | 100 |
tiny-lru.cjs | 100 | 96.34 | 100 | 100 | 190,225,245
--------------|---------|----------|---------|---------|-------------------
Benchmarks
Tiny-LRU includes a comprehensive benchmark suite for performance analysis and comparison. The benchmark suite uses modern Node.js best practices and popular benchmarking tools.
Benchmark Files
Modern Benchmarks (modern-benchmark.js
) ⭐
Comprehensive benchmark suite using Tinybench
Features:
- Statistically analyzed latency and throughput values
- Standard deviation, margin of error, variance calculations
- Proper warmup phases and statistical significance
- Realistic workload scenarios
Test categories:
- SET operations: Empty cache, full cache, eviction scenarios
- GET operations: Hit/miss patterns, access patterns
- Mixed operations: Real-world 80/20 read-write scenarios
- Special operations: Delete, clear, different data types
- Memory usage analysis
Performance Observer Benchmarks (performance-observer-benchmark.js
)
Native Node.js performance measurement using Performance Observer
Features:
- Function-level timing using
performance.timerify()
- PerformanceObserver for automatic measurement collection
- Custom high-resolution timer implementations
- Scalability testing across different cache sizes
Running Benchmarks
npm run benchmark:all
npm run benchmark:modern
npm run benchmark:perf
node benchmarks/modern-benchmark.js
node benchmarks/performance-observer-benchmark.js
node --expose-gc benchmarks/modern-benchmark.js
Understanding Results
Tinybench Output
┌─────────┬─────────────────────────────┬─────────────────┬────────────────────┬──────────┬─────────┐
│ (index) │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
├─────────┼─────────────────────────────┼─────────────────┼────────────────────┼──────────┼─────────┤
│ 0 │ 'set-random-empty-cache-100'│ '2,486,234' │ 402.21854775934 │ '±0.45%' │ 1243117 │
- ops/sec: Operations per second (higher is better)
- Average Time: Average execution time in nanoseconds
- Margin: Statistical margin of error
- Samples: Number of samples collected for statistical significance
Performance Observer Output
┌─────────────┬─────────┬────────────┬────────────┬────────────┬───────────────┬─────────┬────────┐
│ Function │ Calls │ Avg (ms) │ Min (ms) │ Max (ms) │ Median (ms) │ Std Dev │Ops/sec │
├─────────────┼─────────┼────────────┼────────────┼────────────┼───────────────┼─────────┼────────┤
│ lru.set │ 1000 │ 0.0024 │ 0.0010 │ 0.0156 │ 0.0020 │ 0.0012 │ 417292 │
Performance Tips
For accurate benchmark results:
- Close other applications to reduce system noise
- Run multiple times and compare results
- Use consistent hardware for comparisons
- Enable garbage collection with
--expose-gc
for memory tests
- Consider CPU frequency scaling on laptops
Good Performance Indicators
- ✅ Consistent ops/sec across runs
- ✅ Low margin of error (< 5%)
- ✅ GET operations faster than SET
- ✅ Cache hits faster than misses
See benchmarks/README.md
for complete documentation and advanced usage.
API Reference
Properties
first
{Object|null}
- Item in first (least recently used) position
const cache = lru();
cache.first;
last
{Object|null}
- Item in last (most recently used) position
const cache = lru();
cache.last;
max
{Number}
- Maximum number of items to hold in cache
const cache = lru(500);
cache.max;
resetTtl
{Boolean}
- Whether to reset TTL on each set()
operation
const cache = lru(500, 5*6e4, true);
cache.resetTtl;
size
{Number}
- Current number of items in cache
const cache = lru();
cache.size;
ttl
{Number}
- TTL in milliseconds (0 = no expiration)
const cache = lru(100, 3e4);
cache.ttl;
Methods
clear()
Removes all items from cache.
Returns: {Object}
LRU instance
cache.clear();
delete(key)
Removes specified item from cache.
Parameters:
Returns: {Object}
LRU instance
cache.set('key1', 'value1');
cache.delete('key1');
console.log(cache.has('key1'));
See also: has(), clear()
entries([keys])
Returns array of cache items as [key, value]
pairs.
Parameters:
keys
{Array}
- Optional array of specific keys to retrieve (defaults to all keys)
Returns: {Array}
Array of [key, value]
pairs
cache.set('a', 1).set('b', 2);
console.log(cache.entries());
console.log(cache.entries(['a']));
See also: keys(), values()
evict()
Removes the least recently used item from cache.
Returns: {Object}
LRU instance
cache.set('old', 'value').set('new', 'value');
cache.evict();
See also: setWithEvicted()
expiresAt(key)
Gets expiration timestamp for cached item.
Parameters:
Returns: {Number|undefined}
Expiration time (epoch milliseconds) or undefined if key doesn't exist
const cache = new LRU(100, 5000);
cache.set('key1', 'value1');
console.log(cache.expiresAt('key1'));
See also: get(), has()
get(key)
Retrieves cached item and promotes it to most recently used position.
Parameters:
Returns: {*}
Item value or undefined if not found/expired
cache.set('key1', 'value1');
console.log(cache.get('key1'));
console.log(cache.get('nonexistent'));
See also: set(), has()
has(key)
Checks if key exists in cache (without promoting it).
Parameters:
Returns: {Boolean}
True if key exists and is not expired
cache.set('key1', 'value1');
console.log(cache.has('key1'));
console.log(cache.has('nonexistent'));
See also: get(), delete()
keys()
Returns array of all cache keys in LRU order (first = least recent).
Returns: {Array}
Array of keys
cache.set('a', 1).set('b', 2);
cache.get('a');
console.log(cache.keys());
See also: values(), entries()
set(key, value)
Stores item in cache as most recently used.
Parameters:
key
{String}
- Item key
value
{*}
- Item value
Returns: {Object}
LRU instance
cache.set('key1', 'value1')
.set('key2', 'value2')
.set('key3', 'value3');
See also: get(), setWithEvicted()
setWithEvicted(key, value)
Stores item and returns evicted item if cache was full.
Parameters:
key
{String}
- Item key
value
{*}
- Item value
Returns: {Object|null}
Evicted item {key, value, expiry, prev, next}
or null
const cache = new LRU(2);
cache.set('a', 1).set('b', 2);
const evicted = cache.setWithEvicted('c', 3);
if (evicted) {
console.log(`Evicted: ${evicted.key}`, evicted.value);
}
See also: set(), evict()
values([keys])
Returns array of cache values.
Parameters:
keys
{Array}
- Optional array of specific keys to retrieve (defaults to all keys)
Returns: {Array}
Array of values
cache.set('a', 1).set('b', 2);
console.log(cache.values());
console.log(cache.values(['a']));
See also: keys(), entries()
Examples
Basic Usage
import {lru} from "tiny-lru";
const cache = lru(100);
cache.set('key1', 'value1');
console.log(cache.get('key1'));
cache.set("user:123", {name: "John", age: 30})
.set("session:abc", {token: "xyz", expires: Date.now()});
const user = cache.get("user:123");
console.log(cache.size);
TTL with Auto-Expiration
import {LRU} from "tiny-lru";
const cache = new LRU(50, 5000);
cache.set("temp-data", {result: "computed"});
setTimeout(() => {
console.log(cache.get("temp-data"));
}, 6000);
Reset TTL on Access
const cache = lru(100, 10000, true);
cache.set("session", {user: "admin"});
License
Copyright (c) 2025 Jason Mulligan
Licensed under the BSD-3 license.