HTML Renderer Performance Tips — Faster Layouts & Painting
- Prefer compositable properties for animations: animate only
transform,opacity, and certainfiltervalues so the browser can skip layout/paint and run on the compositor (GPU). - Avoid layout-triggering changes during interactions: don’t animate or frequently change
width,height,top,left,margin,padding,flexor other geometry properties. - Minimize style recalculations: change classes or CSS custom properties instead of many inline style mutations; batch DOM writes and reads (use the read–write pattern) to avoid forced synchronous layouts.
- Reduce paint cost: simplify visual complexity (fewer shadows, gradients, large images, complex clipping), avoid oversized paint areas, and use smaller/optimized images and SVGs.
- Manage layers thoughtfully: use
will-changeor create layers (e.g.,transform: translateZ(0)) only for elements that need them to isolate repaints—but remove/habitually avoid over-layering (memory + raster cost). - Limit repaint areas: prefer updating small elements or using composited transforms so only tiny regions are repainted; use contain/contain: paint when applicable to confine layout/paint scope.
- Defer non-critical work: lazy-load offscreen images/resources and defer heavy scripts so initial layout/paint is on the critical path only.
- Use hardware-accelerated rasterization wisely: prefer 3D transforms (e.g., translateZ) for smoother motion, but test on low-end devices because GPU memory and rasterization can be a bottleneck.
- Measure with DevTools: use Performance, Rendering, Paint Profiler, Layers, and FPS throttling to find layout/paint hotspots and verify fixes on real devices.
- Adopt RAIL goals: keep animation frames <16ms, response work small, and move expensive tasks to idle time or web workers where possible.
Quick checklist to speed a slow frame
Leave a Reply