Created
April 19, 2018 00:55
-
-
Save erikjung/e122712695ab7a7c8373870078189af1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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