Created
June 25, 2023 17:13
-
-
Save alekstar79/11125248f8dc609f5c8dcba3e0f75b5e to your computer and use it in GitHub Desktop.
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
/** | |
* @param {Function} fn | |
* @return {Function} | |
*/ | |
export function createCachedFunction(fn) | |
{ | |
const handler = { | |
cache: {}, | |
apply(target, that, args) | |
{ | |
const key = JSON.stringify(args) | |
if (!Object.prototype.hasOwnProperty.call(this.cache, key)) { | |
this.cache[key] = target(...args) | |
} | |
return this.cache[key] | |
} | |
} | |
return new Proxy(fn, handler) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment