diff --git a/src/cache/inmemory/readFromStore.ts b/src/cache/inmemory/readFromStore.ts index 6056cf3f328..1688f4343e3 100644 --- a/src/cache/inmemory/readFromStore.ts +++ b/src/cache/inmemory/readFromStore.ts @@ -222,16 +222,12 @@ export class StoreReader { return result.result; } - const selections = selectionSet.selections.slice(0); - - // Reevaluating selections.length each time is important, because we - // add additional selections in the fragment case below. - for (let s = 0; s < selections.length; ++s) { - const selection = selections[s]; + const workSet = new Set(selectionSet.selections); + workSet.forEach(selection => { // Omit fields with directives @skip(if: ) or // @include(if: ). - if (!shouldInclude(selection, variables)) continue; + if (!shouldInclude(selection, variables)) return; if (isField(selection)) { let fieldValue = policies.readField( @@ -305,10 +301,10 @@ export class StoreReader { } if (policies.fragmentMatches(fragment, typename)) { - selections.push(...fragment.selectionSet.selections); + fragment.selectionSet.selections.forEach(workSet.add, workSet); } } - } + }); // Perform a single merge at the end so that we can avoid making more // defensive shallow copies than necessary. diff --git a/src/cache/inmemory/writeToStore.ts b/src/cache/inmemory/writeToStore.ts index e782bb12686..6087b58068e 100644 --- a/src/cache/inmemory/writeToStore.ts +++ b/src/cache/inmemory/writeToStore.ts @@ -183,14 +183,10 @@ export class StoreWriter { } const { policies } = this; - const selections = selectionSet.selections.slice(0); + const workSet = new Set(selectionSet.selections); - // Reevaluating selections.length each time is important, because we - // add additional selections in the fragment case below. - for (let s = 0; s < selections.length; ++s) { - const selection = selections[s]; - - if (!shouldInclude(selection, context.variables)) continue; + workSet.forEach(selection => { + if (!shouldInclude(selection, context.variables)) return; if (isField(selection)) { const resultFieldKey = resultKeyNameFromField(selection); @@ -255,10 +251,10 @@ export class StoreWriter { ); if (policies.fragmentMatches(fragment, typename)) { - selections.push(...fragment.selectionSet.selections); + fragment.selectionSet.selections.forEach(workSet.add, workSet); } } - } + }); return mergedFields; }