Memoizerific
Memoizerific is a fast, small, and efficient JavaScript memoization library designed to cache function results, preventing re-execution for identical arguments. Currently at version 1.11.3, it receives regular minor updates, with recent changes focusing on package configuration and documentation. A key differentiator is its use of JavaScript's native `Map()` object (with a performant polyfill where unavailable) for instant lookups, avoiding costly serialization or string manipulation common in other memoization approaches. It supports multiple complex arguments and incorporates Least-Recently-Used (LRU) caching, allowing developers to specify a limit on the number of results stored. The library is built for compatibility across both browser and Node.js environments and is particularly useful in scenarios like Redux for deriving data on the fly. Its small footprint (1kb min/gzip) makes it a lightweight choice.
Common errors
-
TypeError: memoizedFunction is not a function
cause This error typically occurs if `memoizerific` is not correctly imported as a default export, or if the factory pattern `memoizerific(limit)(fn)` is not followed, leading to an attempt to call a non-function.fixEnsure `memoizerific` is imported as a default export (e.g., `import memoizerific from 'memoizerific';` for ESM or `const memoizerific = require('memoizerific');` for CJS) and that it's correctly used in the `memoizerific(limit)(yourFunction)` pattern. -
My memoized function keeps running even with identical object arguments!
cause Memoizerific uses strict equality (`===`) for argument comparison. If you are passing new object or array instances to your memoized function on each call, even if their contents are identical, they are considered different arguments by the cache.fixTo get a cache hit with complex arguments (objects, arrays), you must pass the exact same object reference to the memoized function. Ensure your object/array arguments are stable (i.e., not new instances created on every render or function call).
Warnings
- gotcha Arguments passed to a memoized function are compared using strict equality (`===`). For objects and arrays, this means they must refer to the exact same instance in memory to trigger a cache hit, not just have identical properties or content. Creating new object/array literals on each call will result in cache misses.
- gotcha Setting the cache `limit` argument to `0` will result in infinite caching. While sometimes desired, this can lead to unbounded memory consumption, especially in long-running applications or with functions called frequently with varied arguments, potentially causing memory leaks or out-of-memory errors.
- breaking In version 1.10.0, the `main` field in `package.json` was changed to point to the source file (`index.js`) rather than a pre-compiled distribution file, and a new `browser` field was added. While intended to improve compatibility, this change *could* potentially break older bundlers or custom build setups that relied on the previous `main` entry pointing to a pre-compiled output, or setups that don't correctly resolve `browser` fields.
Install
-
npm install memoizerific -
yarn add memoizerific -
pnpm add memoizerific
Imports
- memoizerific
import { memoizerific } from 'memoizerific'import memoizerific from 'memoizerific'
- memoizerific (CJS)
const memoizerific = require('memoizerific') - memoizedFunction
const memoized = memoizerific(50)(myFunction);
Quickstart
import memoizerific from 'memoizerific';
// memoize the 50 most recent argument combinations of our function
const memoized = memoizerific(50)(function(arg1, arg2, arg3) {
// Simulate a long expensive call
console.log(`Processing with args: ${arg1}, ${arg2}, ${arg3}`);
let sum = 0;
for(let i=0; i<1000000; i++) sum += i; // busy loop
return `Result for ${arg1}, ${arg2}, ${arg3}: ${sum}`;
});
console.log(memoized(1, 2, 3)); // that took long to process
console.log(memoized(1, 2, 3)); // this one was instant!
const
complexArg1 = { a: { b: { c: 99 }}},
complexArg2 = [{ z: 1}, { q: [{ x: 3 }]}],
complexArg3 = new Set();
console.log(memoized(complexArg1, complexArg2, complexArg3)); // slow
console.log(memoized(complexArg1, complexArg2, complexArg3)); // instant!