Sunny-117/js-challenges

实现内存函数缓存函数调用结果

Sunny-117 opened this issue · 2 comments

实现内存函数缓存函数调用结果
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);

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));
}
}