Course contentsShow
← Web Development
Lesson 2572 of 2,98246. Animation LibrariesFree lesson

Performance Best Practices

Optimize Framer Motion animations by using transform properties and avoiding layout thrashing.

Performance Best Practices

You'll learn: How to optimize Framer Motion animations by leveraging transform properties and preventing layout thrashing for buttery-smooth animations.

Why Performance Matters in Framer Motion

Framer Motion makes animations easy, but without care, they can cause janky, stuttering motion. The browser's rendering pipeline is like an assembly line: some CSS properties trigger a full rebuild (layout → paint → composite), while others only affect the final assembly step (composite only). Understanding this difference is crucial for 60fps animations.

Use Transform Properties

Animating x, y, scale, rotate, and opacity is fast because these properties only trigger compositing—the browser's cheapest operation. They're hardware-accelerated and don't force the browser to recalculate element positions or repaint.

// ✅ GOOD - Uses transform properties
<motion.div
  animate={{ x: 100, scale: 1.2, opacity: 0.8 }}
/>

// ❌ AVOID - Triggers layout recalculation
<motion.div
  animate={{ width: 300, left: 100, marginTop: 50 }}
/>

Avoid Layout Thrashing

Layout thrashing occurs when you repeatedly force the browser to recalculate layout within a single frame. Think of it like constantly asking someone to measure a room while you're still moving furniture—inefficient and slow.

With Framer Motion, avoid animating properties like width, height, top, left, or margin. If you must change size or position, use scale and x/y transforms instead. The layout prop you learned earlier is powerful but computationally expensive—use it sparingly and only when transforms can't achieve the effect.

// ✅ Simulate size change with scale
<motion.div animate={{ scale: 1.5 }} />

// ❌ Forces layout recalculation every frame
<motion.div animate={{ width: 300, height: 200 }} />

Key Takeaway: Stick to transform properties (x, y, scale, rotate) and opacity for smooth Framer Motion animations; avoid animating layout-affecting properties like width, height, or positional values to prevent layout thrashing.