Skip to content

Instantly share code, notes, and snippets.

@hongphuc5497
Last active May 11, 2022 07:48
Show Gist options
  • Save hongphuc5497/cf248068cdcc5cf99f8288b551089a03 to your computer and use it in GitHub Desktop.
Save hongphuc5497/cf248068cdcc5cf99f8288b551089a03 to your computer and use it in GitHub Desktop.
function slowProduct(a, b) {
console.time('Execution time')
for (let i = 0; i <= 10000000; i++) { }
console.timeEnd('Execution time')
return console.log(a * b);
}
function memoizeValue(fnc, context) {
let res = {};
return function(...args) {
let argsCache = JSON.stringify(args)
if (!res[argsCache]) {
res[argsCache] = fnc.call(context || this, ...args)
}
return res[argsCache]
}
}
for (let count = 0; count <= 5; count++) {
console.log('normal computing')
slowProduct(192, 192)
console.log('memoized value')
memoizeValue(slowProduct(192, 192))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment