简体中文 | English
A high-performance in-memory LRU cache for Node.js and browsers, implemented with a Map and a doubly linked list to achieve O(1) time complexity for all core operations.
In real-world scenarios, fast-map-cache demonstrates significant performance gains by preventing expensive operations:
- 🚀 Measured Benefits: In the simulated API and CPU-bound workloads included in this repo, caching reduced end-to-end work by roughly 2x to 6x when it could avoid expensive operations. Results depend on hit rate and workload cost.
For a detailed analysis, see the full Performance Report.
- 🚀 High Performance: O(1) time complexity for
get,set, anddelete. - 🛡️ LRU Strategy: Automatically evicts the least recently used items when the cache is full.
- ⏱️ TTL Support:
FastCacheWithTTLprovides support for time-to-live expiration of cache items. - 💪 Type-Safe: Written entirely in TypeScript with zero dependencies.
- 🌐 Universal: Works in both Node.js and browser environments.
- 📊 Stats Tracking: Built-in tracking for hits, misses, and hit rate.
# pnpm (recommended)
pnpm add fast-map-cache
# npm
npm install fast-map-cache
# yarn
yarn add fast-map-cacheimport { createCache } from 'fast-map-cache'
// Create a cache with a maximum size of 2
const cache = createCache<string, number>(2)
cache.set('a', 1)
cache.set('b', 2)
console.log(cache.get('a')) // Output: 1
// Adding 'c' will evict the least recently used item ('b')
cache.set('c', 3)
console.log(cache.get('b')) // Output: undefined
console.log(cache.size) // Output: 2For caches that require items to expire after a certain period, use createCacheWithTTL.
import { createCacheWithTTL } from 'fast-map-cache'
const cache = createCacheWithTTL<string, string>({
maxSize: 100,
ttl: 5000, // 5 seconds
})
cache.set('key', 'value')
setTimeout(() => {
console.log(cache.get('key')) // Output: undefined
}, 6000)Note: TTL is based on the timestamp of the last
set()operation. Aget()does not refresh TTL (i.e., it is not access-renewal).
When using FastCacheWithTTL with the autoCleanup: true option in Node.js, an internal setInterval is used for periodic cleanup. The timer is created with unref() when available, so it will not keep the process alive on its own.
It is still recommended to call destroy() before your application exits to proactively clear resources and avoid potential hangs in long-running tasks or test environments.
const cache = createCacheWithTTL({ maxSize: 100, autoCleanup: true, ttl: 60000 })
// ... your application logic ...
// Before shutting down your application:
cache.destroy()For more advanced use cases, including batch operations and presets, please see the full example file in examples/01-basic-example.ts. You can run it directly with pnpm run:example.
The cache instance created by createCache implements IFastCache. FastCacheWithTTL additionally satisfies IFastTTLCache and exposes cleanup() and destroy().
Retrieves the value for a given key. Returns undefined if the key does not exist or has expired. This operation marks the item as recently used.
Adds or updates a key-value pair. If the key already exists, its value is updated and it is marked as recently used. If the cache is full, the least recently used item is evicted.
Note: While JavaScript's
Mapallows any type as a key, it is recommended to use primitive types (string,number,symbol,bigint) for better performance and predictability.
Deletes a key-value pair. Returns true if the key existed and was deleted, false otherwise.
Checks if a key exists in the cache without updating its "recently used" status. Note: For TTL caches, this will lazily delete the item if it's found to be expired, and then return false.
Clears all items from the cache.
Retrieves multiple values for an array of keys. Returns a Map containing the found key-value pairs. This is a convenience API, not a guaranteed performance optimization.
Adds or updates multiple key-value pairs from an array of entries. This is a convenience API, not a guaranteed performance optimization.
Returns the current number of items in the cache.
Returns the maximum number of items the cache can hold.
Returns an object with cache statistics:
{
hits: number;
misses: number;
hitRate: number; // A value between 0 and 1
size: number;
capacity: number;
expired?: number; // Only for TTL caches
}Manually triggers the cleanup of expired items. Returns the number of items that were removed.
Clears the automatic cleanup timer if autoCleanup was enabled. Useful for proactively releasing timer resources in long-lived processes or tests.
This library is designed for high performance in real-world scenarios. The core value of a cache is not just the raw speed of its get/set operations, but its ability to prevent expensive computations or network requests.
The included benchmarks show clear gains when cache hits avoid expensive work such as simulated API calls or CPU-heavy computations. Actual results depend on hit rate, cache size, and the real cost of the work being skipped.
Contributions are welcome! Please see the Contributing Guide for details on how to get started.
This project is licensed under the MIT License.