memoize3.js 753 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.memoize3 = memoize3;
  6. /**
  7. * Memoizes the provided three-argument function.
  8. */
  9. function memoize3(fn) {
  10. let cache0;
  11. return function memoized(a1, a2, a3) {
  12. if (cache0 === undefined) {
  13. cache0 = new WeakMap();
  14. }
  15. let cache1 = cache0.get(a1);
  16. if (cache1 === undefined) {
  17. cache1 = new WeakMap();
  18. cache0.set(a1, cache1);
  19. }
  20. let cache2 = cache1.get(a2);
  21. if (cache2 === undefined) {
  22. cache2 = new WeakMap();
  23. cache1.set(a2, cache2);
  24. }
  25. let fnResult = cache2.get(a3);
  26. if (fnResult === undefined) {
  27. fnResult = fn(a1, a2, a3);
  28. cache2.set(a3, fnResult);
  29. }
  30. return fnResult;
  31. };
  32. }