Last active
April 21, 2022 17:45
-
-
Save hknab/930460947705a067a51779f28947c80e 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
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