Sub topic 17
Measuring the effect in DevTools
This chapter assumes you can open Chrome DevTools and that you know how contain and content-visibility work. Containment is an optimization, so you should confirm it helps before you keep it. The steps below give you a repeatable way to check.
Set up a fair comparison
Results from a fast laptop on a warm cache can hide the problem. Use the same conditions for every recording:
- Open the page in a fresh window with extensions disabled.
- In the Performance panel, set CPU throttling to 4x slowdown.
- Record the same action each time, such as a reload or a scroll from top to bottom.
- Repeat each recording three times and compare the typical result, not the best one.
Change only one thing between runs. Add content-visibility: auto to one selector, record again, then compare.
Read the Performance panel
After recording, open the Main thread track and look for these entries:
- Recalculate Style is the time spent matching selectors to elements.
- Layout is the time spent computing sizes and positions.
- Paint and Pre-paint cover drawing and preparing the layers.
Select a Layout event and read its details. It shows how many nodes needed layout and how many the browser had to visit. With content-visibility: auto, the initial load should show fewer nodes laid out and a shorter Layout bar. Open the Summary tab for the time range and compare the Rendering and Painting totals between your recordings.
Watch the work as it happens
Open the Rendering tab (Command menu, then "Show Rendering"). Two options help here:
- Paint flashing highlights the regions being repainted. Scroll with it on. A well-contained page repaints only the areas that change.
- Layout Shift Regions flashes blue over areas that moved. Use it to catch scrollbar jumps caused by a bad
contain-intrinsic-size.
Check for layout shift in code
A faster render is not a win if the page jumps. This snippet logs the cumulative layout shift while you scroll:
let shift = 0;
new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
if (!entry.hadRecentInput) shift += entry.value;
}
console.log('Layout shift so far:', shift.toFixed(4));
}).observe({ type: 'layout-shift', buffered: true });Paste it into the Console before you scroll. If the number climbs when sections come into view, your placeholder size is too far from the real height. Adjust the value in contain-intrinsic-size.
Common errors when measuring
- Comparing a cached run with an uncached run. Use the same cache state each time.
- Judging by a single recording. Timings vary between runs.
- Forgetting that
content-visibility: automoves rendering work to scroll time. A better load time can mean small costs while scrolling, so record the scroll as well. - Testing only on a fast machine. Keep throttling on.
Exercise
You add content-visibility: auto to long page sections. The Layout time on load drops, but Layout Shift Regions flashes blue while you scroll. What is the likely cause, and what do you change?
Show answer
The placeholder size in contain-intrinsic-size is far from the real section height, so the page shifts when a section renders. Set contain-intrinsic-size to a value close to the real height, using the auto keyword so the browser remembers the last rendered size.