erikjung
4/19/2018 - 12:55 AM

memoizedAdd.js

export const memoizedAdd = () => {
  const cache = new Map();
  return (...args) => {
    const key = args.join();
    const cached = cache.get(key);
    if (cached) {
      console.log(`Returning cached value for ${key}`);
      return cached;
    } else {
      const result = args.reduce((total, val) => total + val);
      return cache.set(key, result) && result;
    }
  }
}