Created
April 25, 2020 08:44
-
-
Save wizardnet972/5e2bc9d43eb985dd15936f4f39851f9a to your computer and use it in GitHub Desktop.
es6 memory function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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