Skip to content

Instantly share code, notes, and snippets.

@erikjung
Created April 19, 2018 00:55
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 erikjung/e122712695ab7a7c8373870078189af1 to your computer and use it in GitHub Desktop.
Save erikjung/e122712695ab7a7c8373870078189af1 to your computer and use it in GitHub Desktop.
export const memoizedAdd = () => {
const cache = new Map();
return (...args) => {
const key = args.join();
const cached = cache.get(key);
if (cached) {
console.log(`Returning cached value for ${key}`);
return cached;
} else {
const result = args.reduce((total, val) => total + val);
return cache.set(key, result) && result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment