Sub topic 17
Measuring the effect in DevTools
This chapter assumes you know the contain and content-visibility properties from earlier chapters. It also assumes you can open Chrome DevTools and read a basic flame chart. The goal is to treat containment as a change you test, not one you take on faith. You record a baseline, add the property, record again, and compare numbers.
Why measure first
Containment is not free. It adds side effects such as clipped overflow and new stacking contexts. It only pays off when rendering work is the bottleneck. A page with 40 small elements gains nothing from content-visibility: auto. A page with 5,000 feed items may gain a lot.
A good measurement answers three questions:
- How long does the browser spend on style, layout, and paint?
- Which of those costs shrinks after the change?
- Did the change make something else worse, such as scroll jank or layout shifts?
What to look at in the Performance panel
The Performance panel in Chromium-based browsers records a timeline of what the main thread did. The rendering work appears under these names:
| Task name | Pipeline stage | What shrinks when containment works |
|---|---|---|
| Recalculate Style | Style | Fewer elements are matched when the subtree is skipped |
| Layout | Layout | Fewer boxes are measured and positioned |
| Pre-paint | Paint preparation | Fewer property trees are built |
| Paint | Paint | Fewer display items are recorded |
| Layerize and Commit | Compositing | Fewer layers to hand to the compositor |
The Summary tab at the bottom shows totals by category. Look at the "Rendering" and "Painting" rows. The Bottom-Up tab lets you sort activities by self time, so the most expensive stage is easy to find.
In the event log, a Layout entry also reports how many nodes needed layout and how many of those were dirty. A drop in "nodes that need layout" after your change is direct evidence that work was limited.
A test page
Use a page that is slow enough to measure. This script builds 3,000 cards. The query string switches the optimization on, so you can compare both versions with the same file.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Containment test</title>
<style>
body { font-family: sans-serif; margin: 0; }
.card {
padding: 16px;
border-bottom: 1px solid #ccc;
height: 140px;
}
.card p { margin: 4px 0; }
.optimized .card {
content-visibility: auto;
contain-intrinsic-size: auto 140px;
}
</style>
</head>
<body>
<main id="feed"></main>
<script>
if (new URLSearchParams(location.search).has('optimized')) {
document.body.classList.add('optimized');
}
const feed = document.getElementById('feed');
const fragment = document.createDocumentFragment();
for (let i = 0; i < 3000; i++) {
const card = document.createElement('article');
card.className = 'card';
card.innerHTML =
'<h2>Item ' + i + '</h2>' +
'<p>Some body text for item ' + i + '.</p>' +
'<p>More text so the card has several boxes.</p>';
fragment.appendChild(card);
}
feed.appendChild(fragment);
</script>
</body>
</html>Serve the file from a local server so both URLs load the same document. Open index.html for the baseline and index.html?optimized for the test.
Recording a load
Follow the same steps for both versions so the results are comparable.
- Open the page in a new Incognito window, which disables most extensions.
- Open DevTools and go to the Performance panel.
- Set CPU throttling to 4x slowdown. This makes differences easier to see on a fast machine.
- Click the reload-and-record button, and wait for the page to finish loading.
- Stop recording, then select the region that covers the first render.
- Read the Summary tab and note the Rendering and Painting totals.
- Repeat three times and compare medians, because single runs vary.
On the baseline, the browser lays out and paints all 3,000 cards even though about six fit in the viewport. With content-visibility: auto, it skips the off-screen cards, so Layout and Paint should be a small fraction of the baseline.
Seeing what the browser skips
You can confirm that skipping happens without reading timings.
- In the Elements panel, an element whose contents are currently skipped shows a "content-visibility" badge next to it in recent Chrome versions. Clicking the badge toggles the state for testing.
- Scroll the page with the Performance panel recording. You should see short Layout and Paint tasks appear as cards enter the render zone, instead of one large task at the start.
- Enable Rendering > Paint flashing from the Command Menu. Green rectangles show the regions being repainted. With containment, flashes should stay inside the element that changed.
The same flow is easy to see as a sequence of checks:
Change only one thing per loop. If you add contain: paint and content-visibility: auto together, you cannot tell which one helped.
Measuring from code
DevTools shows the timeline. For repeatable checks, you can also measure in JavaScript. The PerformanceObserver API reports layout shifts, which tells you whether contain-intrinsic-size is reserving space well.
let total = 0;
const observer = new PerformanceObserver((list) => {
for (const entry of list.getEntries()) {
if (!entry.hadRecentInput) {
total += entry.value;
}
}
console.log('Cumulative layout shift so far:', total.toFixed(4));
});
observer.observe({ type: 'layout-shift', buffered: true });Paste this into the Console on both versions, then scroll to the bottom. A wrong placeholder size makes the score rise as cards are rendered. A good estimate keeps it near zero.
You can also time a forced layout for a specific change. This snippet measures how long the browser takes to recompute layout after you modify one card.
const card = document.querySelector('.card');
const start = performance.now();
card.style.width = '90%';
card.getBoundingClientRect(); // forces a synchronous layout
const elapsed = performance.now() - start;
console.log('Layout after change:', elapsed.toFixed(2), 'ms');Reading getBoundingClientRect() forces the browser to finish layout before it returns. With a containing parent, the number should be smaller, because fewer boxes are invalidated.
Reading the results honestly
Several things can mislead you.
- Warm caches. The second load may be faster for reasons unrelated to your change. Use a hard reload and compare like with like.
- Scroll cost moves, it does not vanish. Skipped content must be rendered later. Initial load improves, but each card entering the viewport adds a small task. Check that scrolling stays smooth.
- Throttling hides nothing. If the page is fast without throttling, say so. The optimization may not be worth its side effects.
- Other bottlenecks. If scripting or network dominates the timeline, containment will not fix the slowness.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| No difference in timings | The elements are already few, or the work is in scripting | Profile the main thread and look for the real cost |
| Scrollbar jumps while scrolling | `contain-intrinsic-size` is far from the real height | Use a measured average, with the `auto` keyword |
| Cards look blank for a moment | Rendering on entry is expensive per card | Reduce card complexity or enlarge the render margin through smaller sections |
| Results differ between runs | Extensions, caching, or background tabs | Use Incognito and repeat three times |
Exercise
You add content-visibility: auto to 3,000 feed cards. Layout time in the initial load drops by 90%, but the Cumulative Layout Shift score rises from 0.01 to 0.4 when you scroll. What does this tell you, and what should you change?
Show answer
The skipped cards use a placeholder height that does not match their real rendered height, so the page shifts as each card is rendered. Set contain-intrinsic-size to a value close to the real average height, and use the auto keyword (for example auto 140px) so the browser remembers the last rendered size. Then record again and confirm that the shift score falls while the layout savings stay.
Exercise
Name the three steps that make a before-and-after DevTools comparison trustworthy.
Show answer
Keep conditions identical (same page, same throttling, an Incognito window), change only one property between recordings, and repeat each recording several times and compare medians instead of a single run.