Skip to content

Instantly share code, notes, and snippets.

@alekstar79
Created June 25, 2023 17:13
Show Gist options
  • Save alekstar79/11125248f8dc609f5c8dcba3e0f75b5e to your computer and use it in GitHub Desktop.
Save alekstar79/11125248f8dc609f5c8dcba3e0f75b5e to your computer and use it in GitHub Desktop.
/**
* @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