-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
73 lines (57 loc) · 1.64 KB
/
index.js
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Import stylesheets
import './style.css';
// Write Javascript code!
const appDiv = document.getElementById('app');
appDiv.innerHTML = `<h1>JS Memoization</h1>`;
/**
* Generic Memoization function
* @param pure function
* @return decorated Memorized function
*/
function memoize(fn) {
let cache = {};
return (...args) => {
const key = [...args].join('+');
if (cache.hasOwnProperty(key)) {
//console.log('cached', key);
return cache[key];
} else {
//console.log('computed', key);
cache[key] = fn(...args);
return cache[key];
}
};
}
function sum(a, b) {
return a + b;
}
var sum = memoize(sum);
/*console.log(sum(2, 3));
console.log(sum(4, 5));
console.log(sum(2, 3));*/
function fibonacci(n) {
if (n === 0) {
return 0;
} else if (n === 1) {
return 1;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
//console.log(fibonacci);
let computedStart = performance.now();
console.log('Computed - fibonacci 10 = ', fibonacci(10));
console.log('Computed - fibonacci 15 = ', fibonacci(15));
let computedEnd = performance.now();
let computedTime = computedEnd - computedStart;
console.log('Computed Time ',computedTime ,'ms');
var fibonacci = memoize(fibonacci);
//console.log(fibonacci);
let cachedStart = performance.now();
console.log('Memoized - fibonacci 10 = ', fibonacci(10));
console.log('Memoized - fibonacci 15 = ', fibonacci(15));
let cachedEnd = performance.now();
let cachedTime = cachedEnd - cachedStart;
console.log('Cached Time ',cachedTime ,'ms');
let cachedPerf = 100 - (cachedTime / computedTime * 100);
console.log(Math.floor(cachedPerf), '% decrease by Memoization');