Skip to content

Instantly share code, notes, and snippets.

@dewey92
Created February 28, 2019 02:38
Show Gist options
  • Select an option

  • Save dewey92/a49d16c25d3e328167c74486576691d3 to your computer and use it in GitHub Desktop.

Select an option

Save dewey92/a49d16c25d3e328167c74486576691d3 to your computer and use it in GitHub Desktop.
Partial App - Cache - JS
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