forked from bharath12345/ADOBE_REACT_21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
closure.html
40 lines (38 loc) · 1.02 KB
/
closure.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h2> Using Closure for Memoize design Pattern</h2>
<script>
function memoize(fn) {
var cache = {}
return function(arg) {
if(cache[arg]) {
return cache[arg];
} else {
cache[arg] = fn(arg);
return cache[arg];
}
}
}
function fibanocci(no) {
if(no == 0 || no ==1) {
return no;
} else {
return fibanocci(no-1) + fibanocci(no-2);
}
}
var memFib = memoize(fibanocci);
console.time("first");
console.log(memFib(34));
console.timeEnd("first");
console.time("second");
console.log(memFib(34));
console.timeEnd("second");
</script>
</body>
</html>