memoize3.mjs 732 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * Memoizes the provided three-argument function.
  3. */
  4. export default function memoize3(fn) {
  5. var cache0;
  6. function memoized(a1, a2, a3) {
  7. if (!cache0) {
  8. cache0 = new WeakMap();
  9. }
  10. var cache1 = cache0.get(a1);
  11. var cache2;
  12. if (cache1) {
  13. cache2 = cache1.get(a2);
  14. if (cache2) {
  15. var cachedValue = cache2.get(a3);
  16. if (cachedValue !== undefined) {
  17. return cachedValue;
  18. }
  19. }
  20. } else {
  21. cache1 = new WeakMap();
  22. cache0.set(a1, cache1);
  23. }
  24. if (!cache2) {
  25. cache2 = new WeakMap();
  26. cache1.set(a2, cache2);
  27. }
  28. var newValue = fn(a1, a2, a3);
  29. cache2.set(a3, newValue);
  30. return newValue;
  31. }
  32. return memoized;
  33. }