Before Memoize
const calculation = (x) => {
for (let i = 0; i < 10000000; i++) {
x += 1;
}
return x;
};
console.time("1st call");
console.log(calculation(1)); //1st call: 14.338134765625 ms
console.timeEnd("1st call");
After Memoize
let cache = {};
function MemoizeCalculation(x) {
if (x in cache) {
return cache[x];
} else {
cache[x] = calculation(x);
return cache[x];
}
}
console.time("1st call");
console.log(MemoizeCalculation(1));
console.timeEnd("1st call");
console.time("2nd call");
console.log(MemoizeCalculation(1));
console.timeEnd("2nd call");
//Now we don't want to have that global cache object outside the method ..
//we need to put inside the MemoizeCalculation method
// By doing so cache object is always {} for every calculation
/*
function MemoizeCalculation(x) {
let cache = {};
if (x in cache) {
return cache[x];
} else {
cache[x] = calculation(x);
return cache[x];
}
}
*/
Closuer comes into picture
/*CLOSURE comes into picture */
function MemoizeCalculation() {
let cache = {};
return function (x) {
if (x in cache) {
return cache[x];
} else {
cache[x] = calculation(x);
return cache[x];
}
};
}
const memoized = MemoizeCalculation();
//it returns the function ..
//it remembers the cache object ...
//global cache object issue is also resolved...
console.time("1st new call");
console.log(memoized(1));
console.timeEnd("1st new call");
console.time("2nd new call");
console.log(memoized(1));
console.timeEnd("2nd new call");
Closure comes into picture
It returns the function ..
It remembers the cache object ...
Global cache object issue is also resolved...
It remembers the cache object ...
Global cache object issue is also resolved...
/*CLOSURE comes into picture */
function MemoizeCalculation() {
let cache = {};
return function (x) {
if (x in cache) {
return cache[x];
} else {
cache[x] = calculation(x);
return cache[x];
}
};
}
const memoized = MemoizeCalculation();
//it returns the function ..
//it remembers the cache object ...
//global cache object issue is also resolved...
console.time("1st new call");
console.log(memoized(1));
console.timeEnd("1st new call");
console.time("2nd new call");
console.log(memoized(1));
console.timeEnd("2nd new call");
Remove hardcoding calculation method - use call or apply
function MemoizeCalculation(func) {
let cache = {};
return function (x) {
if (x in cache) {
return cache[x];
} else {
/*
All below 4 line works - but idea was to remove hardcoding calculation
Use 2 or 4
*/
// cache[x] = calculation(x); //1
//cache[x] = func.call(this, x); //2
//cache[x] = func.apply(calculation, [x]); //3
cache[x] = func.apply(this, [x]); //4
return cache[x];
}
};
}
const memoized = MemoizeCalculation(calculation);
//it returns the function ..
//it remembers the cache object ...
//global cache object issue is also resolved...
console.time("1st new call");
console.log(memoized(1));
console.timeEnd("1st new call");
console.time("2nd new call");
console.log(memoized(1));
console.timeEnd("2nd new call");
Currying
key points
⦿ Currying is transformation of functions that translates a function from callable as f(x, y, z) into callable as f(x)(y)(z).
1) The first example is straight forward. We have 3 parameters to pass. The result is 56
⦿ Infinite currying - we can pass n number of parameters.
1) Initially a is 5
2) It returns function. Then we pass b whichs is 4. Because b exists. It satisfies the if condition, 20 is passed to infinitmul recursively. It returns function again.
3) Now b is 8. then if condition is satisfied. 160 is passed to the function recursively. Returns function
4) now there is no parameter. It goes to else block and it returns 160 as the result
1) The first example is straight forward. We have 3 parameters to pass. The result is 56
⦿ Infinite currying - we can pass n number of parameters.
1) Initially a is 5
2) It returns function. Then we pass b whichs is 4. Because b exists. It satisfies the if condition, 20 is passed to infinitmul recursively. It returns function again.
3) Now b is 8. then if condition is satisfied. 160 is passed to the function recursively. Returns function
4) now there is no parameter. It goes to else block and it returns 160 as the result
const multiply = (a, b) => a * b;
const curriedMultiply = (a) => (b) => (c) => a * b * c;
console.log(`%c ${multiply(4)}`, "color:red;font-size:20px");
console.log(`%c ${curriedMultiply(4)(7)(2)}`, "color:red;font-size:20px");
const infinitmul = (a) => {
return (b) => {
if (b) return infinitmul(`${a} * ${b}`);
else {
return a;
}
};
};
console.log(`%c ${infinitmul(5)(4)(8)()}`, "color:red;font-size:20px");