Skip to content

Instantly share code, notes, and snippets.

@gitdagray
Last active February 8, 2024 14:45
Show Gist options
  • Save gitdagray/fc87981b00386024b0a4a6de940f0a94 to your computer and use it in GitHub Desktop.
Save gitdagray/fc87981b00386024b0a4a6de940f0a94 to your computer and use it in GitHub Desktop.
A memoize decorator function that is capable of handling multiple parameters
// A memoize decorator function
// that is capable of handling multiple parameters
export const memoize = (fn) => {
const cache = {};
return (...args) => {
if (JSON.stringify(args) in cache) {
// if you want to verify result comes from cache
console.log(cache);
return cache[JSON.stringify(args)];
}
const result = fn(...args);
cache[JSON.stringify(args)] = result;
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment