Skip to content

Commit 4ab455c

Browse files
joseph0926김영훈/ENT ETC TL/TLTkDodo
authored
perf(react-query): useQueries have quadratic performance in relation to the number of queries (#8641)
Co-authored-by: 김영훈/ENT ETC TL/TL <yhoon0926@nhnlink.co.kr> Co-authored-by: Dominik Dorfmeister <office@dorfmeister.cc>
1 parent ce7e31e commit 4ab455c

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

packages/query-core/src/queriesObserver.ts

+6-8
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export class QueriesObserver<
4343
#combinedResult?: TCombinedResult
4444
#lastCombine?: CombineFn<TCombinedResult>
4545
#lastResult?: Array<QueryObserverResult>
46+
#observerMatches: Array<QueryObserverMatch> = []
4647

4748
constructor(
4849
client: QueryClient,
@@ -106,6 +107,7 @@ export class QueriesObserver<
106107
const prevObservers = this.#observers
107108

108109
const newObserverMatches = this.#findMatchingObservers(this.#queries)
110+
this.#observerMatches = newObserverMatches
109111

110112
// set options for the new observers to notify of changes
111113
newObserverMatches.forEach((match) =>
@@ -177,17 +179,15 @@ export class QueriesObserver<
177179
return this.#combineResult(r ?? result, combine)
178180
},
179181
() => {
180-
return this.#trackResult(result, queries)
182+
return this.#trackResult(result, matches)
181183
},
182184
]
183185
}
184186

185187
#trackResult(
186188
result: Array<QueryObserverResult>,
187-
queries: Array<QueryObserverOptions>,
189+
matches: Array<QueryObserverMatch>,
188190
) {
189-
const matches = this.#findMatchingObservers(queries)
190-
191191
return matches.map((match, index) => {
192192
const observerResult = result[index]!
193193
return !match.defaultedQueryOptions.notifyOnChangeProps
@@ -263,10 +263,8 @@ export class QueriesObserver<
263263
#notify(): void {
264264
if (this.hasListeners()) {
265265
const previousResult = this.#combinedResult
266-
const newResult = this.#combineResult(
267-
this.#trackResult(this.#result, this.#queries),
268-
this.#options?.combine,
269-
)
266+
const newTracked = this.#trackResult(this.#result, this.#observerMatches)
267+
const newResult = this.#combineResult(newTracked, this.#options?.combine)
270268

271269
if (previousResult !== newResult) {
272270
notifyManager.batch(() => {

0 commit comments

Comments
 (0)