GSAP Performance Best Practices
What you'll learn: How to optimize GSAP animations by choosing the right properties to animate and avoiding performance pitfalls that cause browser slowdowns.
Why Some Properties Are Faster
Not all CSS properties are created equal when it comes to animation performance. Your browser goes through several steps to render each frame: calculate styles, determine layout (where things are positioned), paint pixels, and composite layers together.
The golden rule: Animate transform and opacity whenever possible. These properties are GPU-accelerated and skip the expensive layout and paint steps—they only trigger compositing, which is incredibly fast.
// ⚡ FAST - Uses transform and opacity
gsap.to(".box", {
x: 200, // transform: translateX()
y: 100, // transform: translateY()
scale: 1.5, // transform: scale()
rotation: 45, // transform: rotate()
opacity: 0.5,
duration: 1
});
// 🐌 SLOW - Forces layout recalculation every frame
gsap.to(".box", {
width: 400, // Triggers layout
left: 200, // Triggers layout
top: 100, // Triggers layout
duration: 1
});
Avoiding Layout Thrashing
Layout thrashing occurs when you rapidly read layout properties (like offsetWidth) and then write to them in a loop, forcing the browser to recalculate layout repeatedly. GSAP handles batching internally, but avoid mixing GSAP animations with manual DOM reads in rapid succession.
Think of it like repeatedly asking "how tall is this?" then changing its height—the browser has to remeasure every time.
Key Takeaway: Stick to transform (x, y, scale, rotation) and opacity for buttery-smooth 60fps animations; avoid animating width, height, top, left, or margin as they trigger expensive layout recalculations every frame.