API Optimierungen
This commit is contained in:
18
src/lib/Memoization.ts
Normal file
18
src/lib/Memoization.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
type MemoizedFunction<T> = (...args: any[]) => T;
|
||||
|
||||
export function memoize<T>(func: (...args: any[]) => T): MemoizedFunction<T> {
|
||||
const cache = new Map<string, T>();
|
||||
|
||||
return (...args: any[]): T => {
|
||||
const key = JSON.stringify(args);
|
||||
|
||||
if (cache.has(key)) {
|
||||
return cache.get(key)!;
|
||||
}
|
||||
|
||||
const result = func(...args);
|
||||
cache.set(key, result);
|
||||
|
||||
return result;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user