feat(virtual-core): skip sync DOM read in measureElement when cached size exists #1183
main fromrefs/pull/1183/head·merged by piecyk This PR skip synchronous DOM reads (offsetWidth/offsetHeight) in the default measureElement when a cached size already exists.
Reading offsetWidth/offsetHeight forces browser layout reflow. On re-renders, this is redundant, the ResizeObserver is already watching the element and will fire if the size changes.
🎯 Changes#
This PR skips the sync DOM read when the item already has a measured size in itemSizeCache, returning the cached value instead. The ResizeObserver is already observing the element and will deliver an accurate size asynchronously if it changed. First mount still does the sync read since no cache exists yet.
itemSizeCache is made public so the standalone measureElement function can access it (same pattern as the existing public elementsCache). Users who need sync DOM reads on every render can pass a custom measureElement.
✅ Checklist#
- I have followed the steps in the Contributing guide.
- I have tested this code locally with
pnpm run test:pr.
🚀 Release Impact#
- This change affects published code, and I have generated a changeset.
- This change is docs/CI/dev-only (no release).
Summary by CodeRabbit#
- Refactor
- Improved rendering performance by reusing cached element sizes to avoid unnecessary synchronous DOM reads and reduce layout reflows during re-renders.
- Made the internal item-size cache accessible, allowing advanced users to inspect and manage cached measurements for fine-grained control and debugging.
- 1
View your CI Pipeline Execution ↗ for commit 3c51175032dd262a0392a4a8163dc918434bb969
Command Status Duration Result nx affected --targets=test:sherif,test:knip,tes...✅ Succeeded 2m 25s View ↗ nx run-many --target=build --exclude=examples/**✅ Succeeded 17s View ↗
☁️ Nx Cloud last updated this comment at
2026-06-02 03:36:38UTCMore templates
- @tanstack/virtual-example-angular-dynamic
- @tanstack/virtual-example-angular-fixed
- @tanstack/virtual-example-angular-infinite-scroll
- @tanstack/virtual-example-angular-padding
- @tanstack/virtual-example-angular-smooth-scroll
- @tanstack/virtual-example-angular-sticky
- @tanstack/virtual-example-angular-table
- @tanstack/virtual-example-angular-variable
- @tanstack/virtual-example-angular-window
- tanstack-lit-virtual-example-dynamic
- tanstack-lit-virtual-example-fixed
- tanstack-react-virtual-example-chat
- tanstack-react-virtual-example-dynamic
- tanstack-react-virtual-example-fixed
- tanstack-react-virtual-example-infinite-scroll
- tanstack-react-virtual-example-padding
- tanstack-react-virtual-example-pretext
- tanstack-react-virtual-example-scroll-padding
- tanstack-react-virtual-example-smooth-scroll
- tanstack-react-virtual-example-sticky
- tanstack-react-virtual-example-table
- tanstack-react-virtual-example-variable
- tanstack-react-virtual-example-window
- tanstack-svelte-virtual-example-dynamic
- tanstack-svelte-virtual-example-fixed
- tanstack-svelte-virtual-example-infinite-scroll
- tanstack-svelte-virtual-example-smooth-scroll
- tanstack-svelte-virtual-example-sticky
- tanstack-svelte-virtual-example-table
- tanstack-vue-virtual-example-dynamic
- tanstack-vue-virtual-example-fixed
- tanstack-vue-virtual-example-infinite-scroll
- tanstack-vue-virtual-example-padding
- tanstack-vue-virtual-example-scroll-padding
- tanstack-vue-virtual-example-smooth-scroll
- tanstack-vue-virtual-example-sticky
- tanstack-vue-virtual-example-table
- tanstack-vue-virtual-example-variable
@tanstack/angular-virtual
npm i https://pkg.pr.new/@tanstack/angular-virtual@1183@tanstack/lit-virtual
npm i https://pkg.pr.new/@tanstack/lit-virtual@1183@tanstack/react-virtual
npm i https://pkg.pr.new/@tanstack/react-virtual@1183@tanstack/solid-virtual
npm i https://pkg.pr.new/@tanstack/solid-virtual@1183@tanstack/svelte-virtual
npm i https://pkg.pr.new/@tanstack/svelte-virtual@1183@tanstack/virtual-core
npm i https://pkg.pr.new/@tanstack/virtual-core@1183@tanstack/vue-virtual
npm i https://pkg.pr.new/@tanstack/vue-virtual@1183commit:
3c51175piecyk merged thisc0b84c8
piecyk closed this
Sign in to join this conversation.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID:
e5b46050-4fe6-4598-bace-493f69566cd5📥 Commits
Reviewing files that changed from the base of the PR and between b1f6f19cf7044cf32cbf453347eb339a5f7e1854 and 3c51175032dd262a0392a4a8163dc918434bb969.
📒 Files selected for processing (2)
.changeset/skip-sync-measure-cached.mdpackages/virtual-core/src/index.ts✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 Walkthrough
Walkthrough#
The Virtualizer exposes its element size cache publicly and updates measureElement to return cached sizes during synchronous measurement calls instead of immediately performing offsetWidth/offsetHeight reads.
Changes#
Measurement Cache Optimization
packages/virtual-core/src/index.tsitemSizeCachevisibility is changed fromprivateto public, allowing external access to the size cache map.packages/virtual-core/src/index.ts,.changeset/skip-sync-measure-cached.mdmeasureElementnow checks the cache first when invoked without a ResizeObserverEntry, returning the cached size instead of reading offsetWidth/offsetHeight synchronously; a changeset documents the behavior and patch bump.Estimated code review effort#
🎯 2 (Simple) | ⏱️ ~8 minutes
Suggested reviewers#
Poem#
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
✨ Finishing Touches
🧪 Generate unit tests (beta)
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