Last active
December 15, 2015 14:09
-
-
Save Tushkiz/5272362 to your computer and use it in GitHub Desktop.
Faster Recursions
The memoizer function will take an initial 'cache' array and the 'operation' function.
It returns a 'self' function that manages the cache store, this 'self' function calls the 'operation' function only when there is a 'cache' miss, ultimately improves performance.
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
var memoizer = function (cache, operation) { | |
var self = function (n) { | |
var result = cache[n]; | |
if (typeof result !== 'number') { | |
result = operation(self, n); | |
cache[n] = result; | |
} | |
return result; | |
}; | |
return self; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment