Created
December 14, 2021 21:54
-
-
Save Ivannnnn/7e4cae7aa76d58f4dc977105aef00747 to your computer and use it in GitHub Desktop.
withMemoization
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
| function withMemoization(func, identifierFunc) { | |
| const cache = new Map() | |
| return (...args) => { | |
| const identifier = identifierFunc(...args) | |
| if (cache.has(identifier)) { | |
| return cache.get(identifier) | |
| } | |
| const result = func(...args) | |
| cache.set(identifier, result) | |
| return result | |
| } | |
| } | |
| /* EXAMPLE | |
| const queryPost = withMemoization( | |
| $.query, | |
| ($post, selector) => $post.getAttribute('data-id') + '-' + selector | |
| ) | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment