Last active
August 29, 2015 14:16
-
-
Save JosePedroDias/f17f668c9517d3b63a4c to your computer and use it in GitHub Desktop.
memoify 1 argument function. this has no criteria to limit cache size. be careful.
This file contains 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
/* | |
* single argument memo where argument cached as string (no objects please) | |
* | |
* @function memo | |
* @param {Function} fn function to memoize | |
* @return {Function} memoized version of the function | |
*/ | |
var memo = function(fn) { | |
return function(a) { | |
if (!fn._memo) { | |
fn._memo = {}; | |
} | |
var v; | |
if (a in fn._memo) { | |
v = fn._memo[a]; | |
} | |
else { | |
v = fn(a); | |
fn._memo[a] = v; | |
} | |
return v; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment