Skip to content

Instantly share code, notes, and snippets.

@Ivannnnn
Created December 14, 2021 21:54
Show Gist options
  • Select an option

  • Save Ivannnnn/7e4cae7aa76d58f4dc977105aef00747 to your computer and use it in GitHub Desktop.

Select an option

Save Ivannnnn/7e4cae7aa76d58f4dc977105aef00747 to your computer and use it in GitHub Desktop.
withMemoization
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