Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified memoize3 and improve type checking #2964

Merged
merged 1 commit into from Mar 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
};
}