Skip to content

Use in-memory queue for faster GC registration#1326

Merged
samwillis merged 8 commits intoTanStack:mainfrom
amiran-gorgazjan:amiran-react-unmount-performance-optimisation
Mar 16, 2026
Merged

Use in-memory queue for faster GC registration#1326
samwillis merged 8 commits intoTanStack:mainfrom
amiran-gorgazjan:amiran-react-unmount-performance-optimisation

Conversation

@amiran-gorgazjan
Copy link
Contributor

@amiran-gorgazjan amiran-gorgazjan commented Mar 3, 2026

Use in-memory queue for faster GC registration

Improves performance for issue: #1325

🎯 Changes

  • Introduced CleanupQueue: Created a singleton in-memory queue (packages/db/src/collection/cleanup-queue.ts) to manage garbage collection tasks. It batches task registrations using microtasks and maintains only a single active setTimeout for the earliest scheduled task.
  • Updated CollectionLifecycleManager: Replaced direct setTimeout and clearTimeout calls for GC timers with the new CleanupQueue.getInstance().schedule() and cancel() methods.
  • Added Tests: Added comprehensive test coverage for the new queue behavior in packages/db/tests/cleanup-queue.test.ts.

Motivation: Creating individual setTimeout calls for every component unmount or collection inactivity can lead to performance issues when many collections are involved. Using a centralized in-memory queue optimizes scheduling by significantly reducing the number of active timeouts created in the runtime.

✅ Checklist

  • I have tested this code locally with pnpm test.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

@changeset-bot
Copy link

changeset-bot bot commented Mar 3, 2026

🦋 Changeset detected

Latest commit: 202efc0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 12 packages
Name Type
@tanstack/db Patch
@tanstack/angular-db Patch
@tanstack/electric-db-collection Patch
@tanstack/offline-transactions Patch
@tanstack/powersync-db-collection Patch
@tanstack/query-db-collection Patch
@tanstack/react-db Patch
@tanstack/rxdb-db-collection Patch
@tanstack/solid-db Patch
@tanstack/svelte-db Patch
@tanstack/trailbase-db-collection Patch
@tanstack/vue-db Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@amiran-gorgazjan amiran-gorgazjan marked this pull request as ready for review March 4, 2026 11:43
@pkg-pr-new
Copy link

pkg-pr-new bot commented Mar 12, 2026

More templates

@tanstack/angular-db

npm i https://pkg.pr.new/@tanstack/angular-db@1326

@tanstack/db

npm i https://pkg.pr.new/@tanstack/db@1326

@tanstack/db-ivm

npm i https://pkg.pr.new/@tanstack/db-ivm@1326

@tanstack/electric-db-collection

npm i https://pkg.pr.new/@tanstack/electric-db-collection@1326

@tanstack/offline-transactions

npm i https://pkg.pr.new/@tanstack/offline-transactions@1326

@tanstack/powersync-db-collection

npm i https://pkg.pr.new/@tanstack/powersync-db-collection@1326

@tanstack/query-db-collection

npm i https://pkg.pr.new/@tanstack/query-db-collection@1326

@tanstack/react-db

npm i https://pkg.pr.new/@tanstack/react-db@1326

@tanstack/rxdb-db-collection

npm i https://pkg.pr.new/@tanstack/rxdb-db-collection@1326

@tanstack/solid-db

npm i https://pkg.pr.new/@tanstack/solid-db@1326

@tanstack/svelte-db

npm i https://pkg.pr.new/@tanstack/svelte-db@1326

@tanstack/trailbase-db-collection

npm i https://pkg.pr.new/@tanstack/trailbase-db-collection@1326

@tanstack/vue-db

npm i https://pkg.pr.new/@tanstack/vue-db@1326

commit: 202efc0

Document the cleanup queue's batching behavior and single-timeout design to make the GC scheduling flow easier to understand.

Made-with: Cursor
Copy link
Collaborator

@samwillis samwillis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for dogging into this @amiran-gorgazjan

I did some quick benchmarking and the speedup is real, and the implementation is great. I've pushed a commit with some jsdoc comments, going to merge it now.

@samwillis samwillis merged commit 43ecbfa into TanStack:main Mar 16, 2026
7 checks passed
Sign up for free to 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