Skip to content

Instantly share code, notes, and snippets.

@JosePedroDias
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JosePedroDias/f17f668c9517d3b63a4c to your computer and use it in GitHub Desktop.
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.
/*
* 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