`lru-memoize` is a utility library for JavaScript and TypeScript that provides simple memoization for pure functions using an LRU (Least Recently Used) cache. It helps optimize performance by storing results of expensive function calls and returning the cached result when the same inputs occur again. The current stable version is 1.1.0, released recently with bug fixes and explicit TypeScript/Flow support. The package features a configurable cache limit, custom equality comparison functions, and an option for deep object comparison for arguments, making it flexible for various use cases. Its primary differentiation lies in its minimalist approach to LRU caching for functions, focusing on purity and avoiding side effects. The release cadence appears to be driven by bug fixes and improvements rather than frequent new features, ensuring stability over rapid, breaking changes.
npm install lru-memoizeVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to apply `lru-memoize` to a pure function, configuring a specific cache limit. It illustrates when a function is executed versus when its result is retrieved from the LRU cache, including how older entries are evicted.
Always ensure that any function passed to `memoize` is pure: its output must depend solely on its inputs, and it should produce no observable side effects.
For deep comparison of object/array arguments, initialize `memoize` with `deepObjects: true` (e.g., `memoize(10, null, true)(myFunc)`). For highly custom comparison logic, you can provide your own `equals` function (e.g., `memoize(10, customEquals)(myFunc)`).
Always explicitly specify a `limit` when initializing `memoize` if you intend to cache more than one unique result. For example, `memoizedFunction = memoize(10)(originalFunction)` will cache up to 10 unique argument sets.
Enable deep object comparison by initializing `memoize` with `deepObjects: true`: `memoizedFunc = memoize(cacheLimit, null, true)(originalFunc);`. Alternatively, provide a custom `equals` function for specific comparison needs.
For ES Modules (e.g., in a modern React/Vue project): `import memoize from 'lru-memoize';`. For CommonJS (e.g., in a Node.js script): `const memoize = require('lru-memoize');`.Increase the cache limit when initializing `memoize` to accommodate more entries. For example, to cache 10 unique results, use: `memoizedFunc = memoize(10)(originalFunc);`.
No dependency data recorded yet.