Skip to content

Instantly share code, notes, and snippets.

@Tushkiz
Last active December 15, 2015 14:09
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 Tushkiz/5272362 to your computer and use it in GitHub Desktop.
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.
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