memoize.js 419 B

12345678910111213
  1. import has from './_has.js';
  2. // Memoize an expensive function by storing its results.
  3. export default function memoize(func, hasher) {
  4. var memoize = function(key) {
  5. var cache = memoize.cache;
  6. var address = '' + (hasher ? hasher.apply(this, arguments) : key);
  7. if (!has(cache, address)) cache[address] = func.apply(this, arguments);
  8. return cache[address];
  9. };
  10. memoize.cache = {};
  11. return memoize;
  12. }