Created
April 6, 2018 11:18
-
-
Save eduardomoroni/3ca1f89863c8d2bb7b255f866ac1faee to your computer and use it in GitHub Desktop.
JS 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(fn) { | |
var cachedArg; | |
var cachedResult; | |
return function(arg) { | |
if (cachedArg === arg) { | |
return cachedResult; | |
} | |
cachedArg = arg; | |
cachedResult = fn(arg); | |
return cachedResult; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment