Skip to content

Instantly share code, notes, and snippets.

@wizardnet972
Created April 25, 2020 08:44
Show Gist options
  • Save wizardnet972/5e2bc9d43eb985dd15936f4f39851f9a to your computer and use it in GitHub Desktop.
Save wizardnet972/5e2bc9d43eb985dd15936f4f39851f9a to your computer and use it in GitHub Desktop.
es6 memory function
// created by @wizardnet972
function memes(fn) {
const fns = new Map();
return (...args) => {
const key = `f-${args.length}`;
if (!fns.has(key)) {
const value = fn(...args);
fns.set(key, value);
}
return fns.get(key);
};
}
const x = memes(() => {
console.log("in fn");
return Math.random();
});
console.log({ x1: x() });
console.log({ x2: x() });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment