Skip to content

Use in-memory queue for faster GC registration#1326

Open
amiran-gorgazjan wants to merge 6 commits intoTanStack:mainfrom
amiran-gorgazjan:amiran-react-unmount-performance-optimisation
Open

Use in-memory queue for faster GC registration#1326
amiran-gorgazjan wants to merge 6 commits intoTanStack:mainfrom
amiran-gorgazjan:amiran-react-unmount-performance-optimisation

Conversation

@amiran-gorgazjan
Copy link

@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: 2791b6b

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
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.

1 participant