Skip to content

Instantly share code, notes, and snippets.

@doasync
Last active January 7, 2022 16:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doasync/9e206b55226147a48825e896c7094e9f to your computer and use it in GitHub Desktop.
Save doasync/9e206b55226147a48825e896c7094e9f to your computer and use it in GitHub Desktop.
Create cached function (memoize)
export const cached = (fn) => {
const cache = new Map();
return function cachedFn(...args) {
const input = JSON.stringify(args);
if (cache.has(input)) {
return cache.get(input);
}
const result = fn.apply(this, args);
cache.set(input, result);
return result;
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment