Skip to content

perf(query-core): Reduce observer removal overhead - #11214

Merged
TkDodo merged 2 commits into
TanStack:mainfrom
scttcper:scttcper/optimize-observer-removal
Aug 18, 2026
Merged

perf(query-core): Reduce observer removal overhead#11214
TkDodo merged 2 commits into
TanStack:mainfrom
scttcper:scttcper/optimize-observer-removal

Conversation

@scttcper

@scttcperscttcper commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Removing observers currently allocates a new observer array for every unsubscribe. This removes the observer in place instead, which avoids repeated copies when lots of components share a query.

The hot case is many observers sharing one query. In a local jsdom benchmark with 10,000 disabled useQuery components on the same query:

OperationBeforeAfter
Change every query key190 ms81 ms
Unmount163 ms15 ms

10,000 different queries are effectively unchanged because each query only has one observer:

OperationBeforeAfter
Mount65.9 ms66.9 ms
Parent rerender52.9 ms54.0 ms
Unmount8.8 ms7.8 ms

Summary by CodeRabbit

  • Performance

    • Reduced unnecessary observer churn when subscriptions are removed.
    • Preserved existing cancellation, garbage-collection, and cache notification behavior.
  • Release

    • Included in a patch release of the query core package.

Removing observers copied the full observer list on every unsubscribe, which got expensive when thousands of components shared a query. Splice the existing list in place instead.
Co-Authored-By: Codex <noreply@openai.com>
@coderabbitai

coderabbitaiBot commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: f10b6740-c016-44ae-a9e5-b4db5d071ab6

📥 Commits

Reviewing files that changed from the base of the PR and between 279bb55 and 3d27d3a.

📒 Files selected for processing (2)
  • .changeset/faster-query-observer-churn.md
  • packages/query-core/src/query.ts

Included review availability: Your plan includes up to 10 reviews per rolling hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

removeObserver now removes matching observers in place. A patch changeset documents the @tanstack/query-core change and its reduced observer churn.

Changes

Observer removal

Layer / File(s)Summary
In-place observer removal
packages/query-core/src/query.ts, .changeset/faster-query-observer-churn.md
removeObserver uses splice to remove one matching observer. The changeset records a patch release for @tanstack/query-core.

Estimated code review effort: 1 (Trivial) | ~2 minutes

Merge Risk:⚪ Minimal · up to 3d27d

This localized observer-removal optimization is merge-ready after normal checks and review; no actionable merge-blocking risk remains.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check nameStatusExplanationResolution
Description check⚠️ WarningThe description explains the change and benchmarks but omits the required Changes, Checklist, and Release Impact sections and their confirmations.Add the required template sections and confirm the contributing guide, local tests, and changeset or explain why the change is dev-only.
✅ Passed checks (4 passed)
Check nameStatusExplanation
Title check✅ PassedThe title clearly and concisely describes the main performance optimization in query-core.
Docstring Coverage✅ PassedNo functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check✅ PassedCheck skipped because no linked issues were found for this pull request.
Out of Scope Changes check✅ PassedCheck skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@scttcper
scttcper marked this pull request as ready for review August 18, 2026 00:27
@nx-cloud

nx-cloudBot commented Aug 18, 2026

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 1c5e8bc

CommandStatusDurationResult
nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded7m 47sView ↗
nx run-many --target=build --exclude=examples/*...✅ Succeeded2m 1sView ↗

☁️ Nx Cloud last updated this comment at 2026-08-18 07:37:53 UTC

@pkg-pr-new

Copy link
Copy Markdown
More templates

@tanstack/angular-query-experimental

npm i https://pkg.pr.new/@tanstack/angular-query-experimental@11214

@tanstack/eslint-plugin-query

npm i https://pkg.pr.new/@tanstack/eslint-plugin-query@11214

@tanstack/lit-query

npm i https://pkg.pr.new/@tanstack/lit-query@11214

@tanstack/preact-query

npm i https://pkg.pr.new/@tanstack/preact-query@11214

@tanstack/preact-query-devtools

npm i https://pkg.pr.new/@tanstack/preact-query-devtools@11214

@tanstack/preact-query-persist-client

npm i https://pkg.pr.new/@tanstack/preact-query-persist-client@11214

@tanstack/query-async-storage-persister

npm i https://pkg.pr.new/@tanstack/query-async-storage-persister@11214

@tanstack/query-broadcast-client-experimental

npm i https://pkg.pr.new/@tanstack/query-broadcast-client-experimental@11214

@tanstack/query-core

npm i https://pkg.pr.new/@tanstack/query-core@11214

@tanstack/query-devtools

npm i https://pkg.pr.new/@tanstack/query-devtools@11214

@tanstack/query-persist-client-core

npm i https://pkg.pr.new/@tanstack/query-persist-client-core@11214

@tanstack/query-sync-storage-persister

npm i https://pkg.pr.new/@tanstack/query-sync-storage-persister@11214

@tanstack/react-query

npm i https://pkg.pr.new/@tanstack/react-query@11214

@tanstack/react-query-devtools

npm i https://pkg.pr.new/@tanstack/react-query-devtools@11214

@tanstack/react-query-next-experimental

npm i https://pkg.pr.new/@tanstack/react-query-next-experimental@11214

@tanstack/react-query-persist-client

npm i https://pkg.pr.new/@tanstack/react-query-persist-client@11214

@tanstack/solid-query

npm i https://pkg.pr.new/@tanstack/solid-query@11214

@tanstack/solid-query-devtools

npm i https://pkg.pr.new/@tanstack/solid-query-devtools@11214

@tanstack/solid-query-persist-client

npm i https://pkg.pr.new/@tanstack/solid-query-persist-client@11214

@tanstack/svelte-query

npm i https://pkg.pr.new/@tanstack/svelte-query@11214

@tanstack/svelte-query-devtools

npm i https://pkg.pr.new/@tanstack/svelte-query-devtools@11214

@tanstack/svelte-query-persist-client

npm i https://pkg.pr.new/@tanstack/svelte-query-persist-client@11214

@tanstack/vue-query

npm i https://pkg.pr.new/@tanstack/vue-query@11214

@tanstack/vue-query-devtools

npm i https://pkg.pr.new/@tanstack/vue-query-devtools@11214

commit: 1c5e8bc

@TkDodo
TkDodo merged commit 5bb089d into TanStack:mainAug 18, 2026
9 checks passed
@github-actionsgithub-actionsBot mentioned this pull request Aug 18, 2026
TkDodo pushed a commit that referenced this pull request Aug 20, 2026
Removing an observer in place can shift the active notification loop and skip the next observer. Iterate over a shallow copy so synchronous unsubscriptions keep the old behavior without giving up the faster removal path from #11214.
Co-authored-by: Codex <noreply@openai.com>
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@scttcper@TkDodo