Created
January 10, 2016 21:18
-
-
Save cameronbourke/49e798be4f2add8f27cf to your computer and use it in GitHub Desktop.
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
const memoize = (fn) => { | |
let cache = {}, key; | |
return (...args) => { | |
key = JSON.stringify(args); | |
return cache[key] || (cache[key] = fn.call(null, ...args)); | |
} | |
}; |
Hey! That's a great point. I guess it doesn't make sense to talk about memoizing functions that do not take any arguments. The other problem with this approach is that JSON.stringify()
is a relatively slow call to make!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
what if the function do not have any arument ?
the output cache may look like { '[]': '...'}