-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay31FunctionCache.html
41 lines (37 loc) · 1011 Bytes
/
Day31FunctionCache.html
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
39
40
41
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Day 31</title>
</head>
<body>
<script>
//function level cache ---- memoization
function square(...x){
return x;
}
function memo(fn){
let cachemap=new Map();
return function(...x)
{
let key=[...x].join('-');
if(cachemap.has(key))
{
console.log('from memo');
return cachemap.get(key);
}
let result =fn(...x);
cachemap.set(key,result);
return result;
};
}
let fastfun=memo(square);
console.log(fastfun(5));
console.log(fastfun(5));
console.log(fastfun(5,6,6));
console.log(fastfun(5,6,6));
console.log(fastfun(5,6,6));
</script>
</body>
</html>