Cache function results based on arguments.
new Map()cache.hasfunction memoize(fn) {
const cache = new Map();
return (arg) => {
if (cache.has(arg)) return cache.get(arg);
const res = fn(arg);
cache.set(arg, res);
return res;
};
}