Skip to content

Instantly share code, notes, and snippets.

@arnonate
Created September 18, 2020 19:30
Show Gist options
  • Save arnonate/271786985b8ae44982110992e7ffca32 to your computer and use it in GitHub Desktop.
Save arnonate/271786985b8ae44982110992e7ffca32 to your computer and use it in GitHub Desktop.
// Memoization - caching the result of a function (return value)
// Factorial (!) - Ex: 5! === 5 * 4 * 3 * 2 * 1
// Task 1: Write a function, times10, that takes an argument, n, and multiples n times 10
// a simple multiplication fn
const times10 = n => {
return n * 10;
};
console.log("~~~~~~~~~~~~~~TASK 1~~~~~~~~~~~~~~");
console.log("times10 returns:", times10(9));
// Task 2: Use an object to cache the results of your times10 function.
// protip 1: Create a function that checks if the value for n has been calculated before.
// protip 2: If the value for n has not been calculated, calculate and then save the result in the cache object.
const cache = {};
const memoTimes10 = n => {
// if (cache[n]) return cache[n];
if (n in cache) return cache[n];
cache[n] = n * 10;
return cache[n];
};
console.log("~~~~~~~~~~~~~~TASK 2~~~~~~~~~~~~~~");
console.log("Task 2 calculated value:", memoTimes10(9)); // calculated
console.log("Task 2 cached value:", memoTimes10(9)); // cached
// Task 3: Clean up your global scope by moving your cache inside your function.
// protip: Use a closure to return a function that you can call later.
// A closure is the combination of a function bundled together (enclosed) with
// references to its surrounding state (the lexical environment). In other words,
// a closure gives you access to an outer function’s scope from an inner function.
// In JavaScript, closures are created every time a function is created, at function creation time.
// You have a closure when a function accesses variables defined outside of it.
const times10Closure = n => {
const cache = {};
return n => {
if (n in cache) return cache[n];
cache[n] = n * 10;
return cache[n];
};
};
const memoizedTimes10Closure = times10Closure();
console.log("~~~~~~~~~~~~~~TASK 3~~~~~~~~~~~~~~");
try {
console.log("Task 3 calculated value:", memoizedTimes10Closure(9)); // calculated
console.log("Task 3 cached value:", memoizedTimes10Closure(9)); // cached
} catch (e) {
console.error("Task 3:", e);
}
// Task 4: Make your memo function generic and accept the times10 function as a callback
// rather than defining the n * 10 logic inside the if/else or pulling it in from the global scope.
// protip: Take advantage of the fact that parameters are saved in the closure as well,
// just like the cache from the previous example.
const memoize = cb => {
const cache = {};
return n => {
if (n in cache) return cache[n];
cache[n] = cb(n);
return cache[n];
};
};
// returned function from memoizedAdd
const memoizedTimes10 = memoize(times10);
console.log("~~~~~~~~~~~~~~TASK 4~~~~~~~~~~~~~~");
try {
console.log("Task 4 calculated value:", memoizedTimes10(9)); // calculated
console.log("Task 4 cached value:", memoizedTimes10(9)); // cached
} catch (e) {
console.error("Task 4:", e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment