Created
December 21, 2015 19:23
-
-
Save eternal44/82b74a0650ed5caf0f0c to your computer and use it in GitHub Desktop.
Memoize solution
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
| _.memoize = function(func) { | |
| var history = {}; | |
| var result; | |
| return function() { | |
| var args = Array.prototype.slice.call(arguments); // to set as object keys | |
| var setResult = function(func){ | |
| result = func.apply(this, args); | |
| }; | |
| if (!history[func]) { | |
| setResult(func); | |
| history[func] = {[args]: result}; | |
| } else if (!history[func][args]){ | |
| setResult(func); | |
| history[func][args] = result; | |
| } | |
| return history[func][args]; | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment