[Bug]: Eliminate forced reflow during carousel initialization#1327
Open
dlabadini wants to merge 1 commit intodavidjerleke:masterfrom
Open
[Bug]: Eliminate forced reflow during carousel initialization#1327dlabadini wants to merge 1 commit intodavidjerleke:masterfrom
dlabadini wants to merge 1 commit intodavidjerleke:masterfrom
Conversation
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
NodeHandler.getRects()unconditionally writescontainer.style.transform = 'none'before readingoffsetWidth/offsetHeight, then restores it. This write-then-read pattern forces a synchronous browser reflow on everyactivate()call — even on initial mount when no transform has been applied yet (transformis'').Additionally,
activate()callsengine.translate.to()(which writesstyle.transform) beforeengine.resizeHandler.init()(which readsoffsetWidth/offsetHeight), causing a second forced reflow per carousel init.On pages with multiple carousels, these compound: each carousel init forces 2+ synchronous reflows, blocking the main thread during hydration.
Fix
getRects: Guard the transform write — only mutatestyle.transformwhen it's actually set to something other than'none'or''. On initialactivate(), the transform is empty, so the guard skips the write entirely and theoffsetWidth/offsetHeightreads don't force reflow.activate: MoveresizeHandler.init()andcontainer.offsetParent(reads) beforetranslate.to()andscrollOptimizer.optimize()(writes). Neither read depends on the translate position. This batches all layout reads before any layout writes.Impact
reActivate()after scrolling, the transform guard correctly fires when a transform is presentVerification
Chrome DevTools Performance panel before/after on a page with 9 Embla carousels: