Skip to content

Instantly share code, notes, and snippets.

@spektraldevelopment
Created September 27, 2018 13:52
Show Gist options
  • Save spektraldevelopment/503026dba04395c304bf3a83722fce95 to your computer and use it in GitHub Desktop.
Save spektraldevelopment/503026dba04395c304bf3a83722fce95 to your computer and use it in GitHub Desktop.
JS: Memoize
function memoize(fn) {
const cache = {};
return function (...args) {
//fast version of function
if(cache[args]) {
//if args are already in cache, return result
return cache[args];
}
//Use slow function
const result = fn.apply(this, args);
cache[args] = result;
return result;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment