DOM Manipulation and CSS Properties
Reads that force the browser to lay out again mid-frame, and animations that make it lay out on every
frame. Web and desktop only.
Optimize DOM reads and writes, and never interleave them
All of the below properties or methods, when requested/called in JavaScript, will trigger the browser to synchronously calculate the style and layout*. This is also called reflow or layout thrashing, and is common performance bottleneck.
Generally, all APIs that synchronously provide layout metrics will trigger forced reflow / layout. Read on for additional cases and details
Getting box metrics
elem.offsetLeft, elem.offsetTop, elem.offsetWidth, elem.offsetHeight, elem.offsetParent
elem.clientLeft, elem.clientTop, elem.clientWidth, elem.clientHeight
elem.getClientRects(), elem.getBoundingClientRect()
Scroll stuff
elem.scrollBy(), elem.scrollTo()
elem.scrollIntoView(), elem.scrollIntoViewIfNeeded()
elem.scrollWidth, elem.scrollHeight
elem.scrollLeft, elem.scrollTop also, setting them
elem.focus() (source)
elem.computedRole, elem.computedName
elem.innerText (source)
window.scrollX, window.scrollY
window.innerHeight, window.innerWidth
window.visualViewport.height / width / offsetTop / offsetLeft (source)
document.scrollingElement only forces style
document.elementFromPoint
inputElem.focus()
inputElem.select(), textareaElem.select()
mouseEvt.layerX, mouseEvt.layerY, mouseEvt.offsetX, mouseEvt.offsetY (source)
window.getComputedStyle() will typically force style recalc.
window.getComputedStyle() will often force layout, as well.
Details of the conditions where gCS() forces layout
window.getComputedStyle() will force layout in one of 3 conditions:
- The element is in a shadow tree
- There are media queries (viewport-related ones). Specifically, one of the following: (source
min-width, min-height, max-width, max-height, width, height
aspect-ratio, min-aspect-ratio, max-aspect-ratio
device-pixel-ratio, resolution, orientation , min-device-pixel-ratio, max-device-pixel-ratio
- The property requested is one of the following: (source)
height, width
top, right, bottom, left
margin [-top, -right, -bottom, -left, or shorthand] only if the margin is fixed.
padding [-top, -right, -bottom, -left, or shorthand] only if the padding is fixed.
transform, transform-origin, perspective-origin
translate, rotate, scale
grid, grid-template, grid-template-columns, grid-template-rows
perspective-origin
- These items were previously in the list but appear to not be any longer (as of Feb 2018):
motion-path, motion-offset, motion-rotation, x, y, rx, ry
range.getClientRects(), range.getBoundingClientRect()
SVG
Quite a lot of properties/methods force. This list in incomplete:
SVGLocatable: computeCTM(), getBBox()
SVGTextContent: getCharNumAtPosition(), getComputedTextLength(), getEndPositionOfChar(), getExtentOfChar(), getNumberOfChars(), getRotationOfChar(), getStartPositionOfChar(), getSubStringLength(), selectSubString()
SVGUse: instanceRoot
Lots & lots of stuff, including copying an image to clipboard (source)
Source
// bad - collapse.tsx:37 - line two re-reads the clientWidth that line one's write just invalidated
inner.style.width = `${inner.clientWidth}px`;
container.style.width = `${inner.clientWidth}px`;
// good - one read, then the writes
const innerWidth = inner.clientWidth;
inner.style.width = `${innerWidth}px`;
container.style.width = `${innerWidth}px`;
- Where geometry can come from an observer, take it there: an
IntersectionObserver or ResizeObserver
callback runs after layout, so reading in it is cheap (example: useAnchor.ts:25).
requestAnimationFrame requests the browser to call a user-supplied callback function before the next repaint.
Transition compositor properties, and never leave the property unnamed
width, height, top, margin and min-width re-run layout on every frame of the animation
transform, opacity are composited and do not.
transition: all is the same defect with nothing named — and so is the bare-duration shorthand, because an omitted transition-property resets to all.
// bad - ProgressBar.tsx:22 - lays out the bar every frame for half a second
width: ${({ $max, $value }) => `calc((100% / ${$max}) * ${$value})`};
transition: width 0.5s;
// good - laid out once at full width, then scaled
width: 100%;
transform-origin: left;
transform: scaleX(${({ $max, $value }) => $value / $max});
transition: transform 0.5s;
transform-origin is load-bearing: the default origin is the centre, so without it every bar in the app
grows from its middle. Scale is also the weakest case for transform — a scale change re-rasters unless
will-change: transform pins the texture, and then it stretches a bitmap — so it is safe for a solid
childless fill like this one and wrong for a pill-shaped or labelled bar, where it distorts the radius and
the text. Translation and opacity are the genuinely composite-only changes.
Related skills
1---2name: performance-dom3description: Browser rendering performance for Trezor Suite — forced synchronous layout from geometry reads, observer APIs instead of measuring an element, and transitioning only compositor properties. Use when measuring, scrolling or animating a DOM element, or when writing a CSS transition. Web and desktop only.4metadata:5 source: https://skillmd.com/skills/trezor/performance-dom6---78# DOM Manipulation and CSS Properties910Reads that force the browser to lay out again mid-frame, and animations that make it lay out on every11frame. Web and desktop only.1213## Optimize DOM reads and writes, and never interleave them1415All of the below properties or methods, when requested/called in JavaScript, will trigger the browser to synchronously calculate the style and layout*. This is also called reflow or layout thrashing, and is common performance bottleneck.1617Generally, all APIs that synchronously provide layout metrics will trigger forced reflow / layout. Read on for additional cases and details1819Getting box metrics2021- `elem.offsetLeft`, `elem.offsetTop`, `elem.offsetWidth`, `elem.offsetHeight`, `elem.offsetParent`22- `elem.clientLeft`, `elem.clientTop`, `elem.clientWidth`, `elem.clientHeight`23- `elem.getClientRects()`, `elem.getBoundingClientRect()`2425Scroll stuff2627- `elem.scrollBy()`, `elem.scrollTo()`28- `elem.scrollIntoView()`, `elem.scrollIntoViewIfNeeded()`29- `elem.scrollWidth`, `elem.scrollHeight`30- `elem.scrollLeft`, `elem.scrollTop` also, setting them3132- `elem.focus()` ([source](https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/dom/element.cc;l=4206-4225;drc=d685ea3c9ffcb18c781bc3a0bdbb92eb88842b1b))3334- `elem.computedRole`, `elem.computedName`35- `elem.innerText` ([source](https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/editing/element_inner_text.cc;l=462-468;drc=d685ea3c9ffcb18c781bc3a0bdbb92eb88842b1b))3637- `window.scrollX`, `window.scrollY`38- `window.innerHeight`, `window.innerWidth`39- window.visualViewport.height / width / offsetTop / offsetLeft ([source](https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/frame/visual_viewport.cc;l=435-461;drc=a3c165458e524bdc55db15d2a5714bb9a0c69c70?originalUrl=https:%2F%2Fcs.chromium.org%2F))4041- `document.scrollingElement` only forces style42- `document.elementFromPoint`4344- `inputElem.focus()`45- `inputElem.select()`, `textareaElem.select()`4647- `mouseEvt.layerX`, `mouseEvt.layerY`, `mouseEvt.offsetX`, `mouseEvt.offsetY` ([source](https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/events/mouse_event.cc;l=476-487;drc=52fd700fb07a43b740d24595d42d8a6a57a43f81))4849`window.getComputedStyle()` will typically force style recalc.5051`window.getComputedStyle()` will often force layout, as well.5253Details of the conditions where gCS() forces layout5455`window.getComputedStyle()` will force layout in one of 3 conditions:56571. The element is in a shadow tree582. There are media queries (viewport-related ones). Specifically, one of the following: ([source](https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/css/media_query_exp.cc;l=240-256;drc=4c8db70889f2d2fae8338b16f553c646dd20bf78)59 - `min-width`, `min-height`, `max-width`, `max-height`, `width`, `height`60 - `aspect-ratio`, `min-aspect-ratio`, `max-aspect-ratio`61 - `device-pixel-ratio`, `resolution`, `orientation` , `min-device-pixel-ratio`, `max-device-pixel-ratio`623. The property requested is one of the following: ([source](https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/css/properties/css_property.h;l=69;drc=d685ea3c9ffcb18c781bc3a0bdbb92eb88842b1b))63 - `height`, `width`64 - `top`, `right`, `bottom`, `left`65 - `margin` [`-top`, `-right`, `-bottom`, `-left`, or _shorthand_] only if the margin is fixed.66 - `padding` [`-top`, `-right`, `-bottom`, `-left`, or _shorthand_] only if the padding is fixed.67 - `transform`, `transform-origin`, `perspective-origin`68 - `translate`, `rotate`, `scale`69 - `grid`, `grid-template`, `grid-template-columns`, `grid-template-rows`70 - `perspective-origin`71 - These items were previously in the list but appear to not be any longer (as of Feb 2018): `motion-path`, `motion-offset`, `motion-rotation`, `x`, `y`, `rx`, `ry`7273- `range.getClientRects()`, `range.getBoundingClientRect()`7475### SVG7677Quite a lot of properties/methods force. This list in incomplete:7879- SVGLocatable: `computeCTM()`, `getBBox()`80- SVGTextContent: `getCharNumAtPosition()`, `getComputedTextLength()`, `getEndPositionOfChar()`, `getExtentOfChar()`, `getNumberOfChars()`, `getRotationOfChar()`, `getStartPositionOfChar()`, `getSubStringLength()`, `selectSubString()`81- SVGUse: `instanceRoot`8283- Lots & lots of stuff, including copying an image to clipboard ([source](https://source.chromium.org/search?q=UpdateStyleAndLayout%20-f:test&ss=chromium%2Fchromium%2Fsrc:third_party%2Fblink%2Frenderer%2Fcore%2Fediting%2F))8485[Source](https://gist.githubusercontent.com/paulirish/5d52fb081b3570c81e3a/raw/6e6fd7096c505d3b536f0612f3dc91a9dec97302/what-forces-layout.md)8687```ts88// bad - collapse.tsx:37 - line two re-reads the clientWidth that line one's write just invalidated89inner.style.width = `${inner.clientWidth}px`;90container.style.width = `${inner.clientWidth}px`;9192// good - one read, then the writes93const innerWidth = inner.clientWidth;9495inner.style.width = `${innerWidth}px`;96container.style.width = `${innerWidth}px`;97```9899- Where geometry _can_ come from an observer, take it there: an `IntersectionObserver` or `ResizeObserver`100 callback runs after layout, so reading in it is cheap (example: [`useAnchor.ts:25`](../../suite/router/src/useAnchor.ts)).101- `requestAnimationFrame` requests the browser to call a user-supplied callback function before the next repaint.102103## Transition compositor properties, and never leave the property unnamed104105- `width`, `height`, `top`, `margin` and `min-width` re-run layout on every frame of the animation106- `transform`, `opacity` are composited and do not.107- `transition: all` is the same defect with nothing named — and so is the bare-duration shorthand, because an omitted `transition-property` resets to `all`.108109```tsx110// bad - ProgressBar.tsx:22 - lays out the bar every frame for half a second111width: ${({ $max, $value }) => `calc((100% / ${$max}) * ${$value})`};112transition: width 0.5s;113114// good - laid out once at full width, then scaled115width: 100%;116transform-origin: left;117transform: scaleX(${({ $max, $value }) => $value / $max});118transition: transform 0.5s;119```120121- `transform-origin` is load-bearing: the default origin is the centre, so without it every bar in the app122 grows from its middle. Scale is also the weakest case for `transform` — a scale change re-rasters unless123 `will-change: transform` pins the texture, and then it stretches a bitmap — so it is safe for a solid124 childless fill like this one and wrong for a pill-shaped or labelled bar, where it distorts the radius and125 the text. Translation and opacity are the genuinely composite-only changes.126127## Related skills128129- [Asymptotic complexity](../performance-complexity/SKILL.md) — indexing, sorting and reducing over130 collections that grow.131- [React hooks](../performance-react-hooks/SKILL.md) — memoization, dependency arrays, render loops.132- [Long and non-essential tasks](../performance-scheduling/SKILL.md) — yielding long tasks, deferring133 background work.