blob: 2f0e87a17f2e7cabaa56726ea1bdbce3a5844448 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import type { Callable } from '@emprespresso/pengueno';
export const memoize = <R, F extends Callable<R>>(fn: F): F => {
const cache = new Map<string, R>();
return ((...args: unknown[]): R => {
const key = JSON.stringify(args);
if (cache.has(key)) {
return cache.get(key)!;
}
const res = fn(...args);
cache.set(key, res);
return res;
}) as F;
};
|