memoize.js 436 B

123456789101112131415
  1. var _has = require('./_has.js');
  2. // Memoize an expensive function by storing its results.
  3. 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. }
  13. module.exports = memoize;