Created
July 31, 2020 14:56
-
-
Save raybellis/5f1206644c8f70acd2f3a11b0f9a65eb to your computer and use it in GitHub Desktop.
Generic memoize function supporting multiple parameter key lookups
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
function memoize(factory, ctx, mapper) { | |
const cache = new Map(); | |
return function(...keys) { | |
const key = mapper ? mapper(keys) : keys[0]; | |
if (!cache.has(key)) { | |
cache.set(key, factory.apply(ctx, keys)); | |
} | |
console.debug(cache); | |
return cache.get(key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
example usage: