Skip to content

Commit

Permalink
Simplified memoize3 and improve type checking (#2964)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Mar 13, 2021
1 parent aa80e1b commit 26e4568
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/jsutils/memoize3.js
Expand Up @@ -10,29 +10,28 @@ export function memoize3<
let cache0;

return function memoized(a1, a2, a3) {
if (!cache0) {
if (cache0 === undefined) {
cache0 = new WeakMap();
}

let cache1 = cache0.get(a1);
let cache2;
if (cache1) {
cache2 = cache1.get(a2);
if (cache2) {
const cachedValue = cache2.get(a3);
if (cachedValue !== undefined) {
return cachedValue;
}
}
} else {
if (cache1 === undefined) {
cache1 = new WeakMap();
cache0.set(a1, cache1);
}
if (!cache2) {

let cache2 = cache1.get(a2);
if (cache2 === undefined) {
cache2 = new WeakMap();
cache1.set(a2, cache2);
}
const newValue = fn(a1, a2, a3);
cache2.set(a3, newValue);
return newValue;

let fnResult = cache2.get(a3);
if (fnResult === undefined) {
fnResult = fn(a1, a2, a3);
cache2.set(a3, fnResult);
}

return fnResult;
};
}

0 comments on commit 26e4568

Please sign in to comment.