Repository navigation
Expand file tree
/
Copy pathMemoization-and-Hof.js
More file actions
92 lines (72 loc) · 2.79 KB
/
Copy pathMemoization-and-Hof.js
File metadata and controls
92 lines (72 loc) · 2.79 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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* 💡"JavaScript-with-JC"
👉 Memoization and Higher Order Functions
💡 Memoization is a technique for speeding up applications by caching the results of
expensive function calls and returning them when the same inputs are passed again.
memoization makes applications more efficient and faster.
💡 Higher order functions are functions that either accept functions as an argument or return functions
💡 Some of the Higher order functions that take first class citizen function as an argument.
👉 1) Array.prototype.forEach
👉 2) Array.prototype.filter
👉 3) Array.prototype.map
👉 4) Array.prototype.reduce
👉 5) Array.prototype.some
👉 6) Array.prototype.every
👉 7) Array.prototype.find
👉 8) Array.prototype.findIndex
*/
// 💡 Memoization Example
// Expensive function taking longer time ( Approx. 2 sec )
function squareNumber(number) {
let start = new Date().getTime();
let end = start + 2000;
while (start < end) {
start = new Date().getTime();
}
return number * number;
}
// Higher order function
function memoize(cbFunc) {
// 👇 a cache object
let cache = {};
// 👇 inner anonymous function has access to cache object due to closures
return (...args) => {
// 👇 argsKey is key of cache object, Stringifying array of arguments into key using JSON.stringify
let argsKey = JSON.stringify(args);
// 👇 execute `cbFunc` only if there is no cached value
if (!cache[argsKey]) {
cache[argsKey] = cbFunc(...args);
}
// 👇 return the cached value
return cache[argsKey];
};
}
const memoizedSquare = memoize(squareNumber);
console.time("First function call");
console.log(memoizedSquare(2)); // takes 2 sec, 4
console.timeEnd("First function call"); // First function call: 2.005s
console.time("Second function call");
console.log(memoizedSquare(3)); // takes 2 sec, 9
console.timeEnd("Second function call"); // Second function call: 2.000s
console.time("Third function call");
console.log(memoizedSquare(5)); // takes 2 sec, 25
console.timeEnd("Third function call"); // Third function call: 2.001s
console.time("Fourth function call");
console.log(memoizedSquare(2)); // returns cached value 4
console.timeEnd("Fourth function call"); // Fourth function call: 0.324ms
// 💡 Higher Order Function Example
// Higher order function taking first class citizen function as an argument
function calculate(cbfunc, radius) {
return cbfunc(radius);
}
// first class citizen function
function area(radius) {
return Math.PI * radius * radius;
}
// first class citizen function
function perimeter(radius) {
return 2 * Math.PI * radius;
}
const areaOfCircle = calculate(area, 5);
const perimeterOfCircle = calculate(perimeter, 5);
console.log(areaOfCircle); // 78.53981633974483
console.log(perimeterOfCircle); // 31.41592653589793