Advanced Performance
Rendering Performance
JavaScript execution, layout thrashing, and animation optimization.
Smooth 60fps
Users expect smooth, responsive interfaces. Understanding the rendering pipeline helps you avoid jank.
The Rendering Pipeline
- JavaScript: Execute handlers, update DOM
- Style: Calculate computed styles
- Layout: Calculate geometry (expensive!)
- Paint: Fill in pixels
- Composite: Combine layers
Avoid Layout Thrashing
// Bad - forces layout on each iteration
elements.forEach(el => {
el.style.width = el.offsetWidth + 10 + 'px';
});
// Good - batch read then write
const widths = elements.map(el => el.offsetWidth);
elements.forEach((el, i) => {
el.style.width = widths[i] + 10 + 'px';
});Use CSS for Animations
/* GPU-accelerated properties */
transform: translateX(100px);
opacity: 0.5;
/* Avoid animating */
width, height, top, left, margin, paddingPerformance APIs
requestAnimationFramefor visual updatesrequestIdleCallbackfor non-urgent workIntersectionObserverfor visibility detection