Skip to content

Instantly share code, notes, and snippets.

@mindplay-dk
Last active March 27, 2019 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindplay-dk/511c81fccf31ec8a80e1998e97f2efd1 to your computer and use it in GitHub Desktop.
Save mindplay-dk/511c81fccf31ec8a80e1998e97f2efd1 to your computer and use it in GitHub Desktop.
Memoization function (JavaScript)
function memoized(fn) {
let oldArgs = null;
let cache = null;
return (...newArgs) => {
if (oldArgs === null || newArgs.some((arg, index) => arg !== oldArgs[index])) {
cache = fn(...newArgs);
oldArgs = newArgs;
}
return cache;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment