@eurusik/memoized
Version:
🧠Smart memoization decorator for TypeScript - A lightweight and flexible TypeScript decorator that memoizes method or getter results using deep argument comparison
152 lines (151 loc) • 4.18 kB
TypeScript
/**
* @memoized - Smart memoization decorator for TypeScript
* A lightweight and flexible TypeScript decorator that memoizes method or getter results
* using deep argument comparison.
*/
/**
* Interface for objects with memoized methods
*/
export interface WithMemoizedMethods {
__memoizedClearFns?: Record<string, () => void>;
}
/**
* Deep equality comparison function
* Compares two values recursively for equality
*
* @param a - First value to compare
* @param b - Second value to compare
* @param depth - Maximum recursion depth (default: 100)
* @returns boolean indicating if values are deeply equal
*/
export declare function deepEqual(a: unknown, b: unknown, depth?: number): boolean;
/**
* Options for memoization
*/
export interface MemoizationOptions {
/**
* Time-to-live in milliseconds for cached values
* If specified, cached values will expire after this time
*/
ttl?: number;
}
/**
* Creates a memoized version of a method
*
* @param originalMethod - The original method to memoize
* @param key - The property key of the method
* @param options - Optional memoization options
* @param instance - Optional instance to attach clearMemo function to
* @returns A memoized version of the original method with a clearMemo function
*/
export declare function createMemoizedMethod(originalMethod: (...args: unknown[]) => unknown, key: string, options?: MemoizationOptions, instance?: WithMemoizedMethods): ((...args: unknown[]) => unknown) & {
clearMemo: () => void;
};
/**
* Clears all memoized values on an instance
*
* @param instance - Instance with memoized methods
*/
export declare function clearAllMemoized(instance: WithMemoizedMethods): void;
/**
* Memoization decorator for class methods and getters
* Supports both legacy and stage 3 decorators
*
* @example
* // Legacy decorator
* class Example {
* @memoized
* expensiveMethod(arg1: string, arg2: number) {
* // expensive calculation
* return result;
* }
*
* @memoized
* get expensiveComputation() {
* // expensive calculation
* return result;
* }
* }
*
* @example
* // Stage 3 decorator
* class Example {
* @memoized
* expensiveMethod(arg1: string, arg2: number) {
* // expensive calculation
* return result;
* }
*
* @memoized
* get expensiveComputation() {
* // expensive calculation
* return result;
* }
* }
*/
/**
* Memoization decorator for class methods and getters
* Supports both legacy and stage 3 decorators
*
* @example
* // Legacy decorator
* class Example {
* @memoized
* expensiveMethod(arg1: string, arg2: number) {
* // expensive calculation
* return result;
* }
*
* @memoized
* get expensiveComputation() {
* // expensive calculation
* return result;
* }
* }
*
* @example
* // Stage 3 decorator
* class Example {
* @memoized
* expensiveMethod(arg1: string, arg2: number) {
* // expensive calculation
* return result;
* }
*
* @memoized
* get expensiveComputation() {
* // expensive calculation
* return result;
* }
* }
*/
export declare function memoized<T>(target: object, propertyKey: string, descriptor: TypedPropertyDescriptor<T>): TypedPropertyDescriptor<T>;
export declare function memoized<A extends unknown[], R>(target: (...args: A) => R, context: ClassGetterDecoratorContext | ClassMethodDecoratorContext): (...args: A) => R;
/**
* Time-based memoization decorator for class methods and getters
* Automatically expires cached values after the specified time-to-live (TTL)
* Supports both legacy and stage 3 decorators
*
* @param ttl - Time-to-live in milliseconds for cached values
*
* @example
* // Legacy decorator
* class Example {
* @memoizedTTL(5000)
* expensiveMethod(arg1: string, arg2: number) {
* // expensive calculation
* return result;
* }
* }
*
* @example
* // Stage 3 decorator
* class Example {
* @memoizedTTL(5000)
* expensiveMethod(arg1: string, arg2: number) {
* // expensive calculation
* return result;
* }
* }
*/
export declare function memoizedTTL<T>(ttl: number): any;