{"size": 631, "ext": "js", "max_stars_repo_path": "helpers/memoization.js", "max_stars_repo_name": "lukasz-kowalski/helpers", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "helpers/memoization.js", "max_issues_repo_name": "lukasz-kowalski/helpers", "max_issues_repo_issues_event_min_datetime": "2020-07-16T23:59:19.000Z", "max_issues_repo_issues_event_max_datetime": "2021-05-08T16:37:45.000Z", "max_forks_repo_path": "helpers/memoization.js", "max_forks_repo_name": "lukasz-kowalski/helpers", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 4, "max_forks_count": null, "avg_line_length": 25.24, "max_line_length": 87, "alphanum_fraction": 0.6228209192}
// memoization of pure functions
// creates local cache using closure, storing function arguments as keys in cache.
// returns value from object if cached or calls function with args array (apply method)
// example use: 
// const add = (a, b) => a + b;
// const memoAdd = memoization(add);
// memoAdd(3, 4);
// memoAdd(3, 4);

const memoization = fn => {
  const cache = {};
  return (...args) => {
    const key = JSON.stringify(args);
    if (key in cache) {
      return cache[key];
    } else {
      const value = fn.apply(null, args);
      cache[key] = value;
      return value;
    }
  }
};

module.exports = memoization;
