实现内存函数缓存函数调用结果
Sunny-117 opened this issue · 2 comments
Sunny-117 commented
实现内存函数缓存函数调用结果
z1the3 commented
const cachedFn = (function (fn) {
let cache = {};
return function (...args) {
argsKey = args.toString()
if (argsKey in cache) {
return cache[argsKey];
}
return (cache[argsKey] = fn.apply(this, args));
};
})(fn);
Windseek commented
const map = new Map();
const cacheFn = function(fn) {
return function(...args) {
if( map.get(args)) {
return map.get(args)
}
return map.set(args, fn.appy(this, args));
}
}