AgentSkillsCN

animation-performance

提供React Native Reanimated、React Native Worklets、React Native Gesture Handler的性能优化指南,助力实现流畅的动画效果。本技能适用于涉及共享值、Worklet、帧回调、手势处理器、动画样式、重新渲染,或用于调试动画卡顿与帧率骤降等问题的任务。

SKILL.md
--- frontmatter
name: animation-performance
description: Provides React Native Reanimated, React Native Worklets, React Native Gesture Handler performance optimization guidelines for smooth animations. Applies to tasks involving shared values, worklets, frame callbacks, gesture handlers, animated styles, re-renders, or debugging animation jank and frame drops.

Animation Performance Tips

Overview

Performance optimization guide for React Native Reanimated animations, covering shared values, worklets, memoization, and rendering optimizations. These guidelines ensure buttery smooth animations at 60fps.

When to Apply

Reference these guidelines when:

  • Debugging janky or stuttering animations
  • Optimizing animation performance (FPS drops)
  • Working with shared values and worklets
  • Implementing gesture handlers
  • Optimizing animated lists and scroll handlers
  • Reviewing Reanimated code for performance issues

Priority-Ordered Guidelines

PriorityCategoryImpactPrefix
1Shared Values UsageCRITICALshared-values-*
2Re-rendersCRITICALre-renders-*
3MemoizationHIGHmemoization-*
4Animation CalculationsHIGHcalculations-*
5Layout vs TransformHIGHlayout-*
6ReactionsHIGHreactions-*
7Hooks SelectionHIGHhooks-*
8Animation LifecycleMEDIUManimations-*
9Component LimitsMEDIUMlimits-*
10Scroll OptimizationMEDIUMscroll-*

Quick Reference

Critical: Shared Values Usage

Common mistakes:

  • Reading shared values on JS thread (causes thread blocking)
  • Accessing shared values during render (violates Rules of React)
  • Using component state for animation values (triggers re-renders)

Quick fixes:

  • Read shared values only in worklets (useAnimatedStyle, useDerivedValue) - see shared-values-read-in-worklets.md
  • Access shared values in useEffect or callbacks, not during render - see shared-values-during-render.md
  • Use useSharedValue instead of useState for animation values - see re-renders-use-shared-values.md
  • Track animation state with useRef instead of reading shared values - see shared-values-track-state-with-ref.md
  • Initialize with simple values, compute complex ones in worklets - see shared-values-initialization.md

Critical: Re-renders

Common fixes:

  • Use shared values instead of component state for animations
  • Memoize animation objects outside component or with constants
  • Memoize frame callbacks with useCallback
  • Memoize gesture objects with useMemo

High: Animation Calculations

Common fixes:

  • Use useDerivedValue for expensive calculations - see calculations-use-derived-value.md
  • Avoid conditional logic in worklets - see calculations-avoid-conditionals.md
  • Use clamp instead of manual Math.min/max - see calculations-use-clamp.md
  • Batch updates with scheduleOnUI - see calculations-batch-with-schedule-on-ui.md
  • Prefer transform properties over layout properties - see layout-prefer-transform.md
  • Choose the right hook (useAnimatedProps vs useAnimatedStyle) - see hooks-choose-animated-props.md
  • Optimize useAnimatedReaction dependencies - see reactions-optimize-dependencies.md
  • Cancel animations properly before starting new ones - see animations-cancel-properly.md

References

Full documentation with code examples in references/:

Shared Values (shared-values-*)

FileImpactDescription
shared-values-read-in-worklets.mdCRITICALRead shared values only in worklets, not on JS thread
shared-values-during-render.mdCRITICALDon't read/modify shared values during render
shared-values-track-state-with-ref.mdHIGHTrack animation state with useRef instead of reading shared values
shared-values-initialization.mdHIGHAvoid expensive initial values, use simple primitives

Re-renders (re-renders-*)

FileImpactDescription
re-renders-use-shared-values.mdCRITICALUse shared values instead of component state for animations

Memoization (memoization-*)

FileImpactDescription
memoization-animations.mdHIGHMemoize animation objects to prevent recreation
memoization-frame-callbacks.mdHIGHMemoize frame callbacks with useCallback
memoization-gesture-objects.mdHIGHMemoize gesture objects with useMemo

Calculations (calculations-*)

FileImpactDescription
calculations-use-derived-value.mdHIGHUse useDerivedValue to optimize useAnimatedStyle calculations
calculations-avoid-conditionals.mdHIGHMinimize conditional logic in worklets, use useDerivedValue
calculations-use-clamp.mdHIGHUse clamp instead of manual Math.min/max in worklets
calculations-batch-with-schedule-on-ui.mdHIGHBatch shared value updates with scheduleOnUI

Layout (layout-*)

FileImpactDescription
layout-prefer-transform.mdHIGHPrefer transform over layout properties

Reactions (reactions-*)

FileImpactDescription
reactions-optimize-dependencies.mdHIGHAvoid unnecessary reactions, optimize dependency tracking

Hooks (hooks-*)

FileImpactDescription
hooks-choose-animated-props.mdHIGHChoose useAnimatedProps vs useAnimatedStyle correctly

Animations (animations-*)

FileImpactDescription
animations-cancel-properly.mdMEDIUMCancel running animations before starting new ones

Limits (limits-*)

FileImpactDescription
limits-animated-components.mdMEDIUMLimit number of simultaneously animated components

Scroll (scroll-*)

FileImpactDescription
scroll-handlers-throttle.mdMEDIUMOptimize scroll handlers with throttling

Problem → Skill Mapping

ProblemStart With
Animation feels janky/stutteringshared-values-read-in-worklets.mdre-renders-use-shared-values.md
Reading shared value to determine toggle stateshared-values-track-state-with-ref.md
Too many re-renders during animationre-renders-use-shared-values.mdmemoization-animations.md
Expensive calculations every framecalculations-use-derived-value.mdcalculations-avoid-conditionals.md
Conditional logic in worklets causing jankcalculations-avoid-conditionals.md
Layout animations are slowlayout-prefer-transform.md
Multiple shared value updates cause jankcalculations-batch-with-schedule-on-ui.md
Clamping values inefficientlycalculations-use-clamp.md
Frame callback performance issuesmemoization-frame-callbacks.md
Gesture handler reattaches frequentlymemoization-gesture-objects.md
useAnimatedReaction causing performance issuesreactions-optimize-dependencies.md
Wrong hook choice (useAnimatedProps vs useAnimatedStyle)hooks-choose-animated-props.md
Animations conflicting or not cancelinganimations-cancel-properly.md
Shared value initialization is expensiveshared-values-initialization.md
Too many animated componentslimits-animated-components.md
Scroll handler causes performance issuesscroll-handlers-throttle.md