Home > @cascadiacollections/fluentui-compat > memoizeFunction
memoizeFunction() function
Highly optimized memoization function that caches results based on argument identity. Provides configurable cache size limits and automatic memory management.
Key performance optimizations: - Uses WeakMap for automatic garbage collection of object arguments - Efficient primitive value caching with shared dictionary - Configurable cache size limits to prevent memory leaks - Global reset counter for bulk cache invalidation - Fast argument normalization
Signature:
export declare function memoizeFunction<T extends (...args: any[]) => RetType, RetType>(fn: T, maxCacheSize?: number, ignoreNullOrUndefinedResult?: boolean): T;
Parameters
|
Parameter |
Type |
Description |
|---|---|---|
|
fn |
T |
Function to memoize |
|
maxCacheSize |
number |
(Optional) Maximum cache entries before reset (0 = unlimited, default 100) |
|
ignoreNullOrUndefinedResult |
boolean |
(Optional) Re-compute if result is null/undefined until non-null result |
Returns:
T
Memoized version of the function
Example
const expensiveFunction = memoizeFunction((a: number, b: string) => {
// Expensive computation
return a * b.length + Math.random();
});
// Subsequent calls with same arguments return cached result
const result1 = expensiveFunction(5, "hello");
const result2 = expensiveFunction(5, "hello"); // Cached