Skip to content

Instantly share code, notes, and snippets.

@hknab
Last active April 21, 2022 17:45
Show Gist options
  • Save hknab/930460947705a067a51779f28947c80e to your computer and use it in GitHub Desktop.
Save hknab/930460947705a067a51779f28947c80e to your computer and use it in GitHub Desktop.
function memoization(func) {
let cache = new Map();
const getData = () => {
if (cache[func]) {
console.log("cache");
return cache[func];
} else {
cache[func] = func();
console.log("not cache");
return cache[func];
}
};
return getData;
}
const heavy = memoization(() => {
const arr = [];
for (let i = 0; i < 100000000; i++) {
arr.push(i);
}
return arr;
});
const start = Date.now();
console.log(heavy());
const end = Date.now();
console.log(end - start);
console.log(heavy());
const end2 = Date.now();
console.log(end2 - end);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment