Created
October 25, 2015 00:32
-
-
Save daniellowtw/1ee877e1a3bc2e67cbf5 to your computer and use it in GitHub Desktop.
Javascript memoize decorator functor
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
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