We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a06d8b4 commit 18fd9a1Copy full SHA for 18fd9a1
Memoization.js
@@ -0,0 +1,31 @@
1
+function memoize(fn) {
2
+ const cache = {};
3
+ return (...args) => {
4
+ const arg = JSON.stringify(args);
5
+
6
+ if (arg in cache) {
7
+ console.log("fetching from cache");
8
+ return cache[arg];
9
+ } else {
10
+ console.log("fetching for the first time");
11
+ const result = fn.apply(this, args);
12
+ cache[arg] = result;
13
+ return result;
14
+ }
15
+ };
16
+}
17
18
+const addThreeNums = (a, b, c) => a + b + c;
19
20
+const add = memoize(addThreeNums);
21
+console.time();
22
+console.log(add(1, 2, 3));
23
+console.timeEnd();
24
25
26
27
28
29
30
31
0 commit comments