{"id":11256,"library":"lru-memoize","title":"LRU Memoization Utility","description":"`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.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/erikras/lru-memoize","tags":["javascript","memoize","cache","caching","es7","decorator","typescript"],"install":[{"cmd":"npm install lru-memoize","lang":"bash","label":"npm"},{"cmd":"yarn add lru-memoize","lang":"bash","label":"yarn"},{"cmd":"pnpm add lru-memoize","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary `memoize` function is exported as a default export in ESM environments.","wrong":"import { memoize } from 'lru-memoize';","symbol":"memoize","correct":"import memoize from 'lru-memoize';"},{"note":"This is the standard CommonJS import pattern for Node.js environments.","symbol":"memoize","correct":"const memoize = require('lru-memoize');"}],"quickstart":{"code":"import memoize from 'lru-memoize';\n\n// A \"pure\" function to be memoized. It calculates the product of three numbers.\n// We add a console log to demonstrate when the actual calculation occurs.\nconst multiply = (a: number, b: number, c: number): number => {\n  console.log(`Calculating for ${a}, ${b}, ${c}...`); // Shows when the function actually runs\n  return a * b * c;\n};\n\n// Memoize the 'multiply' function with a cache limit of 2.\n// This means it will remember the last 2 unique sets of arguments and their results.\nconst memoizedMultiply = memoize(2)(multiply);\n\nconsole.log('--- Demonstrating Cache Hits and Evictions ---');\nconsole.log('1. First call:', memoizedMultiply(2, 3, 4)); // Calculates: (24) -> cache: [(2,3,4):24]\nconsole.log('2. Second call:', memoizedMultiply(5, 6, 7)); // Calculates: (210) -> cache: [(5,6,7):210, (2,3,4):24]\nconsole.log('3. First call again (cached):', memoizedMultiply(2, 3, 4)); // Hits cache -> cache: [(2,3,4):24, (5,6,7):210]\nconsole.log('4. New call (evicts oldest):', memoizedMultiply(1, 2, 3)); // Calculates: (6) -> cache: [(1,2,3):6, (2,3,4):24]\nconsole.log('5. Second call again (cached):', memoizedMultiply(5, 6, 7)); // Calculates (was evicted) -> cache: [(5,6,7):210, (1,2,3):6] \nconsole.log('6. First call again (evicted, re-calculates):', memoizedMultiply(2, 3, 4)); // Calculates -> cache: [(2,3,4):24, (5,6,7):210]","lang":"typescript","description":"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."},"warnings":[{"fix":"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.","message":"`lru-memoize` is designed exclusively for pure functions. Memoizing functions that have side effects (e.g., modifying external state, performing network requests) or rely on external mutable state will lead to incorrect or stale results, as the function will not re-execute when its external dependencies change.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"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)`).","message":"By default, `lru-memoize` uses strict equality (`===`) to compare function arguments. If your memoized function receives object or array arguments that might be different instances but contain identical data, they will be treated as distinct cache keys, leading to cache misses.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"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.","message":"The `limit` parameter for the LRU cache defaults to `1`. This means that by default, `lru-memoize` will only store the result of the very last unique set of arguments. Any call with different arguments will evict the previous entry.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"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.","cause":"By default, `lru-memoize` uses strict equality (`===`) for argument comparison. Since distinct object instances (even with identical properties) are not strictly equal, they are treated as unique cache keys.","error":"Function is always re-calculating, not using cache, even when called with object arguments that look identical."},{"fix":"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');`.","cause":"This error typically indicates an incorrect import statement, particularly when mixing ESM `import` syntax with CommonJS `require` expectations, or attempting a named import where a default import is required.","error":"TypeError: memoizedFunction is not a function (or similar import errors like 'memoize is not defined')"},{"fix":"Increase the cache limit when initializing `memoize` to accommodate more entries. For example, to cache 10 unique results, use: `memoizedFunc = memoize(10)(originalFunc);`.","cause":"The default cache `limit` for `lru-memoize` is `1`. This means that only the absolute last unique set of arguments and its computed result are retained in the cache. Any new unique call will replace the single cached entry.","error":"The memoized function only caches the most recent result; previous results are always re-calculated when recalled."}],"ecosystem":"npm"}