Skip to content

⚡ bolt: cache document dimensions to avoid layout thrashing#20

Open
dttdrv wants to merge 1 commit intomainfrom
bolt/cache-dimensions-9692812995039132109
Open

⚡ bolt: cache document dimensions to avoid layout thrashing#20
dttdrv wants to merge 1 commit intomainfrom
bolt/cache-dimensions-9692812995039132109

Conversation

@dttdrv
Copy link
Copy Markdown
Owner

@dttdrv dttdrv commented Mar 19, 2026

💡 what: cached document.documentElement.scrollHeight and window.innerHeight in the scrollprogress and sectionnav components to avoid querying the dom directly during scroll events and intersection observer callbacks. the cache is invalidated via a resizeobserver on the document element, and during window resize events.

🎯 why: querying layout dimensions like scrollHeight and innerHeight inside scroll handlers (even within requestanimationframe) causes synchronous layout thrashing, which is a significant performance bottleneck. this change eliminates those reflows, ensuring smoother scrolling.

📊 impact: eliminates synchronous layout thrashing related to scroll progress and section tracking, improving scroll smoothness and reducing cpu overhead during scrolling.

🔬 measurement: visually verify the scrolling experience and ensure the scroll progress bar and section tracking work properly. profile performance before and after to observe fewer layout recalculations.


PR created automatically by Jules for task 9692812995039132109 started by @dttdrv

…ectionnav

caching these dimensions avoids forced synchronous layouts (layout thrashing) during scroll events and intersection observer callbacks, improving scrolling performance and smoothness. invalidate the cache via resizeobserver and window resize events.
@google-labs-jules
Copy link
Copy Markdown

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

Copilot AI review requested due to automatic review settings March 19, 2026 05:58
@cloudflare-workers-and-pages
Copy link
Copy Markdown

Deploying personal-website with  Cloudflare Pages  Cloudflare Pages

Latest commit: baeca0d
Status: ✅  Deploy successful!
Preview URL: https://1d469e3c.personal-website-5ns.pages.dev
Branch Preview URL: https://bolt-cache-dimensions-969281.personal-website-5ns.pages.dev

View logs

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR aims to improve scroll-related performance by caching layout-dependent document dimensions for the scroll progress bar and section navigation logic, reducing repeated layout reads during scroll/observer callbacks.

Changes:

  • Add cachedDocHeight / cachedWindowHeight caches to ScrollProgress and SectionNav, and switch computations to use cached values.
  • Recompute cached dimensions on debounced window.resize.
  • Add a global ResizeObserver intended to refresh cached dimensions when layout/content changes.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
script.js Introduces cached dimension reads for scroll progress + section nav and adds resize/observer-based invalidation.
.jules/bolt.md Documents the performance learning/action for caching dimensions with ResizeObserver.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment thread .jules/bolt.md

## 2024-05-20 - [Frontend Performance: Caching Dimensions with ResizeObserver]
**Learning:** Reading `document.documentElement.scrollHeight` and `window.innerHeight` frequently inside scroll event handlers (even with `requestAnimationFrame`) causes unnecessary synchronous layout calculations, harming performance and scrolling smoothness.
**Action:** Cache these dimensions in properties. Set up a global `ResizeObserver` on `document.documentElement` to automatically recalculate and update the cached values only when the layout height actually changes (e.g. from expanding content or lazy-loaded images). No newline at end of file
Comment thread script.js
if (typeof ScrollProgress !== 'undefined') ScrollProgress.cacheDimensions();
if (typeof SectionNav !== 'undefined') SectionNav.cacheDimensions();
});
layoutObserver.observe(document.documentElement);
Comment thread script.js
Comment on lines +1823 to +1827
const layoutObserver = new ResizeObserver(() => {
if (typeof ScrollProgress !== 'undefined') ScrollProgress.cacheDimensions();
if (typeof SectionNav !== 'undefined') SectionNav.cacheDimensions();
});
layoutObserver.observe(document.documentElement);
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: baeca0d51e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread script.js
Comment on lines +1823 to +1825
const layoutObserver = new ResizeObserver(() => {
if (typeof ScrollProgress !== 'undefined') ScrollProgress.cacheDimensions();
if (typeof SectionNav !== 'undefined') SectionNav.cacheDimensions();
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Refresh scroll progress when cached heights change

This observer handles the exact non-scroll cases mentioned in the comment, but it only updates cachedDocHeight/cachedWindowHeight. On this page, index.html lazy-loads several gallery images and PageLoad.hidePreloader() also removes DOM after load, so the document height can change while the user is stationary; because ScrollProgress.update() is never called here, the progress bar keeps the old percentage until the next scroll or resize and becomes visibly incorrect.

Useful? React with 👍 / 👎.

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