Skip to content

Instantly share code, notes, and snippets.

@Caballerog
Created April 10, 2020 13:07
Show Gist options
  • Save Caballerog/f19a688883c9ddd003a26b09c5d37797 to your computer and use it in GitHub Desktop.
Save Caballerog/f19a688883c9ddd003a26b09c5d37797 to your computer and use it in GitHub Desktop.
const memoize = fn => {
const cache = {}; // 1
return (...args) => { // 2
const stringfiedArgs = JSON.stringify(args); // 3
const result = (cache[stringifiedArgs] =
typeof cache[stringifiedargs] === 'undefined'
? fn(...args)
: cache[stringifedArgs]); // 4
return result; // 5
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment