The Real Fullstack

The Layout Thrash

No network, no heavy maths, 60 ms API. Why does scrolling feel like wading through mud?

工程师8 分钟性能实验室调试实验室
javascriptdomangularchrome-devtools

此挑战尚未翻译为简体中文,你正在阅读英文版本。

任务:Find what turns one layout per frame into three hundred, and restructure the code so the browser lays out the page once.背景 · 学习目标 ▾

Fernpost's order table keeps row heights in sync while scrolling. A performance trace shows one 420 ms long task per scroll event, made of hundreds of alternating style recalculations and layouts.

任务: Find what turns one layout per frame into three hundred, and restructure the code so the browser lays out the page once.

学习目标

  • Recognise forced synchronous layout (read after write).
  • Batch DOM reads before DOM writes.
  • Tell real fixes from hiding the symptom (debounce, will-change).
模拟模拟数据

运行模拟以查看输出。

运行模拟
onScroll() {  // 1. read everything (one layout)  const heights = this.rows.map((row) => row.scrollHeight);  // 2. write everything in the next frame (no layout in between)  requestAnimationFrame(() => {    this.rows.forEach((row, i) => (row.style.height = heights[i] + 'px'));  });}/* .table { contain: layout; }  keeps the re-layout inside the table */

简化说明

Two symptoms with deterministic fixes. In a real trace you would confirm the fix by re-recording the performance profile. 教学模型——数字用于说明行为,并非基准测试。

概念

相关挑战

参考资料