-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMemoizeFunction.html
More file actions
38 lines (34 loc) · 1.08 KB
/
MemoizeFunction.html
File metadata and controls
38 lines (34 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<script>
function MemoizeFunc(fn, context) {
const res = {};
return function (...args) {
var argsCache
if (!res[argsCache]) {
res[argsCache] = fn.call(context || this, ...args);
}
return res[argsCache];
JSON.stringify(args);
};
}
const clumsyProduct = (num1, num2) => {
for (let i = 1; i <= 100000000; i++) { }
return num1 * num2;
};
const memoizedClumzyProduct = MemoizeFunc(clumsyProduct);
// Before Memoizing
console.time("First call");
console.log(clumsyProduct(9467, 7649));
console.timeEnd("First call");
console.time("Second call");
console.log(clumsyProduct(9467, 7649));
console.timeEnd("Second call");
// Before Memoizing
// After Memoizing
console.time("First call");
console.log(memoizedClumzyProduct(9467, 7649));
console.timeEnd("First call");
console.time("Second call");
console.log(memoizedClumzyProduct(9467, 7649));
console.timeEnd("Second call");
// After Memoizing
</script>