Created
May 11, 2021 18:58
-
-
Save kubarskii/64463c97b73180249b2f0b0fcdee0d39 to your computer and use it in GitHub Desktop.
Memo 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
const memo = (function () { | |
const cache = new Map() | |
return function (fn, ctx = null, ...params) { | |
if (cache.has([fn, params, ctx].toString())) { | |
return cache.get([fn, params, ctx].toString()) | |
} else { | |
const res = fn.call(ctx, ...params); | |
cache.set([fn, params, ctx].toString(), res); | |
return res; | |
} | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment