diff --git a/packages/query-core/src/queriesObserver.ts b/packages/query-core/src/queriesObserver.ts index c9fbc5e164..381e444a7d 100644 --- a/packages/query-core/src/queriesObserver.ts +++ b/packages/query-core/src/queriesObserver.ts @@ -131,33 +131,35 @@ export class QueriesObserver extends Subscribable { queries: QueryObserverOptions[], ): QueryObserverMatch[] { const prevObservers = this.observers + const prevObserversMap = new Map( + prevObservers.map((observer) => [observer.options.queryHash, observer]), + ) + const defaultedQueryOptions = queries.map((options) => this.client.defaultQueryOptions(options), ) const matchingObservers: QueryObserverMatch[] = defaultedQueryOptions.flatMap((defaultedOptions) => { - const match = prevObservers.find( - (observer) => - observer.options.queryHash === defaultedOptions.queryHash, - ) + const match = prevObserversMap.get(defaultedOptions.queryHash) if (match != null) { return [{ defaultedQueryOptions: defaultedOptions, observer: match }] } return [] }) - const matchedQueryHashes = matchingObservers.map( - (match) => match.defaultedQueryOptions.queryHash, + const matchedQueryHashes = new Set( + matchingObservers.map((match) => match.defaultedQueryOptions.queryHash), ) const unmatchedQueries = defaultedQueryOptions.filter( - (defaultedOptions) => - !matchedQueryHashes.includes(defaultedOptions.queryHash), + (defaultedOptions) => !matchedQueryHashes.has(defaultedOptions.queryHash), ) + const matchingObserversSet = new Set( + matchingObservers.map((match) => match.observer), + ) const unmatchedObservers = prevObservers.filter( - (prevObserver) => - !matchingObservers.some((match) => match.observer === prevObserver), + (prevObserver) => !matchingObserversSet.has(prevObserver), ) const getObserver = (options: QueryObserverOptions): QueryObserver => {