Memoize, Currying

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];
  }
}
 */
				
			
				
					/*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 */

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");
				
			
				
					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

				
					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");
				
			

Leave a Comment