Created
February 28, 2019 02:38
-
-
Save dewey92/a49d16c25d3e328167c74486576691d3 to your computer and use it in GitHub Desktop.
Partial App - Cache - JS
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
| const memoize = fn => { | |
| const cache = {} | |
| return (…args) => { | |
| const stringifiedArgs = JSON.stringify(args) | |
| const result = cache[stringifiedArgs] || fn(…args) | |
| cache[stringifiedArgs] = result | |
| return result | |
| } | |
| } | |
| const add = (x, y) => x + y | |
| const memoizedAdd = memoize(add); | |
| memoizedAdd(5, 6) // 11 | |
| memoizedAdd(5, 6) // 11, without recomputing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment