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

[FIX]: useQueries have quadratic performance in relation to the number of queries #8641

Merged
merged 2 commits into from
Feb 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions packages/query-core/src/queriesObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#combinedResult?: TCombinedResult
#lastCombine?: CombineFn<TCombinedResult>
#lastResult?: Array<QueryObserverResult>
#observerMatches: Array<QueryObserverMatch> = []

constructor(
client: QueryClient,
Expand Down Expand Up @@ -106,6 +107,7 @@
const prevObservers = this.#observers

const newObserverMatches = this.#findMatchingObservers(this.#queries)
this.#observerMatches = newObserverMatches

// set options for the new observers to notify of changes
newObserverMatches.forEach((match) =>
Expand Down Expand Up @@ -177,17 +179,15 @@
return this.#combineResult(r ?? result, combine)
},
() => {
return this.#trackResult(result, queries)
return this.#trackResult(result, matches)

Check warning on line 182 in packages/query-core/src/queriesObserver.ts

View check run for this annotation

Codecov / codecov/patch

packages/query-core/src/queriesObserver.ts#L182

Added line #L182 was not covered by tests
},
]
}

#trackResult(
result: Array<QueryObserverResult>,
queries: Array<QueryObserverOptions>,
matches: Array<QueryObserverMatch>,
) {
const matches = this.#findMatchingObservers(queries)

return matches.map((match, index) => {
const observerResult = result[index]!
return !match.defaultedQueryOptions.notifyOnChangeProps
Expand Down Expand Up @@ -263,10 +263,8 @@
#notify(): void {
if (this.hasListeners()) {
const previousResult = this.#combinedResult
const newResult = this.#combineResult(
this.#trackResult(this.#result, this.#queries),
this.#options?.combine,
)
const newTracked = this.#trackResult(this.#result, this.#observerMatches)
const newResult = this.#combineResult(newTracked, this.#options?.combine)

if (previousResult !== newResult) {
notifyManager.batch(() => {
Expand Down
Loading