A memoization function that uses a WeakMap for efficient caching based on object identity
npx @tessl/cli install tessl/npm-emotion--weak-memoize@0.4.0@emotion/weak-memoize is a lightweight memoization utility that leverages JavaScript's WeakMap for efficient caching based on object identity. It accepts a single-argument function and returns a memoized version that caches results using the argument object as the key. The WeakMap-based approach ensures memory efficiency by allowing garbage collection of cached entries when the original objects are no longer referenced elsewhere.
npm install @emotion/weak-memoizeimport weakMemoize from "@emotion/weak-memoize";For CommonJS:
const weakMemoize = require("@emotion/weak-memoize");import weakMemoize from "@emotion/weak-memoize";
// Create a memoized function
const memoizedCalculation = weakMemoize((obj: { value: number }) => {
console.log("Computing expensive operation...");
return { result: obj.value * 2 };
});
// First call - computation runs
const obj1 = { value: 5 };
const result1 = memoizedCalculation(obj1); // Logs: "Computing expensive operation..."
// Second call with same object reference - returns cached result
const result2 = memoizedCalculation(obj1); // No log - cached result
console.log(result1 === result2); // true
// Different object with same value - computation runs again
const obj2 = { value: 5 };
const result3 = memoizedCalculation(obj2); // Logs: "Computing expensive operation..."
console.log(result1 === result3); // false - different object referenceCreates a memoized version of a function that caches results using WeakMap for object-based keys.
/**
* Creates a memoized version of a function using WeakMap for caching
* @param func - Function to memoize, must accept exactly one object argument
* @returns Memoized function that caches results by object identity
*/
function weakMemoize<Arg extends object, Return>(
func: (arg: Arg) => Return
): (arg: Arg) => Return;Type Parameters:
Arg extends object - The argument type, must be a non-primitive (object, function, array, etc.)Return - The return type of the function being memoizedParameters:
func: (arg: Arg) => Return - The function to memoize. Must accept exactly one argument of a non-primitive type.Returns:
(arg: Arg) => Return - A memoized version of the input functionKey Characteristics:
Usage Examples:
import weakMemoize from "@emotion/weak-memoize";
// Memoizing expensive object transformations
const processData = weakMemoize((data: { items: any[] }) => {
return data.items.map(item => expensiveTransformation(item));
});
// Memoizing React component prop calculations
const calculateProps = weakMemoize((config: ComponentConfig) => {
return {
className: generateClassName(config),
styles: computeStyles(config),
handlers: createEventHandlers(config)
};
});
// Memoizing function results based on function identity
const memoizeByFunction = weakMemoize((fn: Function) => {
return fn.toString().length; // Cache function string length
});Type Constraints:
The following types are not allowed as arguments (will cause TypeScript compilation errors):
stringnumberbooleansymbolnullundefinedThe following types are allowed as arguments:
{}any[]() => anyclass instancesMap, Set, Date, etc.Error Handling:
The function does not throw runtime errors, but TypeScript will prevent compilation if:
objectPerformance Considerations: