Skip to content

Instantly share code, notes, and snippets.

@daniellowtw
Created October 25, 2015 00:32
Show Gist options
  • Save daniellowtw/1ee877e1a3bc2e67cbf5 to your computer and use it in GitHub Desktop.
Save daniellowtw/1ee877e1a3bc2e67cbf5 to your computer and use it in GitHub Desktop.
Javascript memoize decorator functor
function memoize(fn) {
var invocationCache = {};
return function() {
var args = Array.prototype.slice.call(arguments);
var cachedValue = invocationCache[args];
if (cachedValue !== undefined) {
return cachedValue;
}
var result = fn.apply(null, arguments);
invocationCache[args] = result;
return result;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment