Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Sunny-117 opened this issue Nov 4, 2022 · 2 comments
Open

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

Sunny-117 opened this issue Nov 4, 2022 · 2 comments

Comments

@Sunny-117
Copy link
Owner

No description provided.

@z1the3
Copy link

z1the3 commented May 5, 2023

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
Copy link

Windseek commented Apr 5, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants