Created
September 27, 2018 13:52
-
-
Save spektraldevelopment/503026dba04395c304bf3a83722fce95 to your computer and use it in GitHub Desktop.
JS: Memoize
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(fn) { | |
const cache = {}; | |
return function (...args) { | |
//fast version of function | |
if(cache[args]) { | |
//if args are already in cache, return result | |
return cache[args]; | |
} | |
//Use slow function | |
const result = fn.apply(this, args); | |
cache[args] = result; | |
return result; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment