
Product
Introducing Tier 1 Reachability: Precision CVE Triage for Enterprise Teams
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
@ianduvall/memoize
Advanced tools
A TypeScript library for memoizing pure functions with infinite cache size and automatic memory management
A TypeScript library for memoizing pure functions with infinite cache size and automatic memory management.
pnpm add @ianduvall/memoize
import { memoizePureFunction } from "@ianduvall/memoize";
const expensiveFunction = (x: number, y: number) => {
// Some expensive computation
console.log("Computing...");
return x + y;
};
const memoized = memoizePureFunction(expensiveFunction);
// First call - executes function
const result1 = memoized(1, 2); // Logs: "Computing..."
// Second call with same arguments - returns cached result
const result2 = memoized(1, 2); // No log, returns cached result
You can provide custom hash functions to control how arguments are compared for caching:
// Single hash function for all arguments
const memoizedWithHash = memoizePureFunction(expensiveFunction, {
hashFunction: (arg) => JSON.stringify(arg),
});
// Different hash functions per argument position with full type inference
const processUser = (
user: { name: string; id: number },
settings: { theme: string },
) => `${user.name} prefers ${settings.theme}`;
const memoizedProcess = memoizePureFunction(processUser, {
hashFunction: [
(user) => user.id, // TypeScript infers: { name: string; id: number }
(settings) => settings.theme, // TypeScript infers: { theme: string }
],
});
// Mixed configuration - some arguments use custom hash, others use reference equality
const memoizedMixed = memoizePureFunction(processUser, {
hashFunction: [
(user) => user.id, // Custom hash for first argument
undefined, // Reference equality for second argument
],
});
Alternative to React's useMemo
hook, memoizePureFunction
can be used to share memoization across usages of a hook. This is useful for hooks that need to do some expensive computation but need to be called in many places.
const calculateSomethingExpensive = memoizePureFunction(
function expensiveComputation(user) {},
);
const useExpensiveUserProperty = () => {
const user = useUser();
return calculateSomethingExpensive(user);
};
This can be more ergonomic than alternative solutions to this problem, like hoisting into a context provider, because it maintains the lazy, pull-based sematics of useMemo
.
import {
memoizePureFunction,
clearCache,
clearGlobalCache,
} from "@ianduvall/memoize";
const fn = (x: number) => x * 2;
const memoized = memoizePureFunction(fn);
// Clear cache for a specific function
clearCache(fn);
// Clear all caches globally
clearGlobalCache();
The memoized function preserves the exact type signature of the original function and provides full type inference for hash functions:
const stringFunction = (x: number): string => x.toString();
const numberFunction = (x: string): number => Number.parseInt(x, 10);
const booleanFunction = (x: number): boolean => x > 0;
const memoizedString = memoizePureFunction(stringFunction);
const memoizedNumber = memoizePureFunction(numberFunction);
const memoizedBoolean = memoizePureFunction(booleanFunction);
// TypeScript will enforce correct types
const str: string = memoizedString(42); // ✅
const num: number = memoizedNumber("123"); // ✅
const bool: boolean = memoizedBoolean(5); // ✅
Hash functions receive correctly typed arguments based on the original function signature:
const processData = (
user: { id: number; name: string },
config: { enabled: boolean },
) => {
return `${user.name}: ${config.enabled}`;
};
const memoized = memoizePureFunction(processData, {
hashFunction: [
(user) => user.id, // user is inferred as { id: number; name: string }
(config) => config.enabled, // config is inferred as { enabled: boolean }
],
});
// TypeScript provides full autocomplete and type checking for hash function parameters
MIT License © 2025 Ian Duvall
FAQs
A TypeScript library for memoizing pure functions with infinite cache size and automatic memory management
The npm package @ianduvall/memoize receives a total of 6 weekly downloads. As such, @ianduvall/memoize popularity was classified as not popular.
We found that @ianduvall/memoize demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.
Research
/Security News
Ongoing npm supply chain attack spreads to DuckDB: multiple packages compromised with the same wallet-drainer malware.
Security News
The MCP Steering Committee has launched the official MCP Registry in preview, a central hub for discovering and publishing MCP servers.