Created
March 27, 2018 19:43
-
-
Save kylehovey/30e0382ee3aac5290508a7b641730bff to your computer and use it in GitHub Desktop.
ES6 Memoize Function
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
function memoize(f) { | |
const cache = new Map; | |
return (...args) => { | |
const key = JSON.stringify(...args); | |
if (!cache.has(key)) { | |
cache.set(key, f(...args)); | |
} | |
return cache.get(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment