-
Notifications
You must be signed in to change notification settings - Fork 4
/
hof6.html
36 lines (32 loc) · 960 Bytes
/
hof6.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
function memo(fn) {
var cache = {};
return function(args) {
if(!cache[args]) {
cache[args] = fn(args);
}
return cache[args];
}
}
function fibanocci(num) {
return (num == 0 || num == 1)? num : fibanocci(num - 1) + fibanocci(num - 2);
}
var memFib = memo(fibanocci); // Closure --> fn : fibanocci and cache: {}
console.time("first");
console.log(memFib(34));
console.timeEnd("first");
console.time("second");
console.log(memFib(34));
console.timeEnd("second");
</script>
</body>
</html>