Skip to content

Instantly share code, notes, and snippets.

@keif
Created February 10, 2021 21:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keif/3d6073bae1f7a20b0ae44bdd509b6971 to your computer and use it in GitHub Desktop.
Save keif/3d6073bae1f7a20b0ae44bdd509b6971 to your computer and use it in GitHub Desktop.
Multiple Argument Memoization Function
const constructPropertyFromArgs = function (fn, args) {
return [].concat(fn.name, args).join('|');
}
const memoize = function (fn) {
const cache = {}
return function(...args) {
const propCheck = constructPropertyFromArgs(fn, args);
if (!cache[propCheck]) {
cache[propCheck] = fn(...args);
}
return cache[propCheck];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment