--- title: Reactivity description: Learn how to effectively use Vue's reactivity system with TresJS while maintaining optimal performance in high-frequency render loops. --- ## Understanding Reactivity in 3D Vue's reactivity system is one of its most powerful features, automatically tracking changes and updating the UI accordingly. However, when working with 3D scenes that render at 60+ frames per second, we need to be mindful of how reactivity impacts performance. TresJS leverages Vue's reactivity while providing patterns that maintain optimal performance in continuous render loops. ## The Performance Challenge ### Vue Reactivity Under the Hood Vue's reactivity is built on [JavaScript Proxies](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy), which intercept property access and mutations to track dependencies and trigger updates. ```js [reactivity-basics.js] // Vue creates a Proxy wrapper around your data const data = reactive({ x: 0, y: 0, z: 0 }) // Every property access is intercepted data.x = 5 // Triggers reactivity system console.log(data.y) // Also intercepted for dependency tracking ``` ### The 60FPS Problem In a typical 3D scene running at 60 FPS, the render loop executes 60 times per second. If you're updating reactive objects in each frame, Vue's reactivity system processes these changes 60 times per second: ```vue [performance-problem.vue] ``` ### Performance Impact Here's a benchmark comparing reactive vs non-reactive object updates: :::card-group ::card{title="Plain Object" icon="i-lucide-zap"} **~50M operations/second** Direct property access without proxy overhead :: ::card{title="Reactive Object" icon="i-lucide-turtle"} **~2M operations/second** Proxy interception adds significant overhead :: ::: *Source: [Proxy vs Plain Object Performance](https://www.measurethat.net/Benchmarks/Show/12503/0/object-vs-proxy-vs-proxy-setter)* ## Template Refs: The Preferred Approach Template refs provide direct access to Three.js instances without reactivity overhead, making them ideal for animations and frequent updates. ### Basic Template Ref Usage ```vue [template-refs.vue] ``` ### Multiple Template Refs For complex scenes with multiple animated objects: ```vue [multiple-refs.vue] ``` ## Shallow Reactivity: When You Need Some Reactivity Sometimes you need reactivity for UI controls while maintaining performance for animations. `shallowRef` and `shallowReactive` provide the perfect balance. ### shallowRef vs ref ::code-group ```vue [shallowRef (Recommended)] ``` ```vue [ref (Problematic)] ``` :: ### shallowReactive for Object Properties When you need to reactively update multiple properties independently: ```vue [shallow-reactive.vue] ``` ## Best Practices and Patterns ### 1. Initial Positioning vs Animation Use reactive props for initial positioning and template refs for animation: ```vue [best-practices.vue] ``` ### 2. Computed Properties for Complex Calculations Use computed properties for expensive calculations that shouldn't run in every frame: ```vue [computed-properties.vue] ``` ### 3. Lifecycle-Based Updates Use Vue's lifecycle hooks for performance-sensitive updates: ```vue [lifecycle-updates.vue] ``` ## Common Pitfalls and Solutions ### ❌ Pitfall 1: Reactive Animation Data ```vue [pitfall-reactive-animation.vue] ``` **Solution: Use template refs** ```vue [solution-template-refs.vue] ``` ### ❌ Pitfall 2: Deep Reactive Arrays ```vue [pitfall-reactive-arrays.vue] ``` **Solution: Non-reactive data with template refs** ```vue [solution-particle-system.vue] ``` ## Performance Monitoring ::examples-performance-monitor :: Use `@tresjs/leches` [built-in fpsgraph](https://tresleches.tresjs.org/misc/#fpsgraph) for monitoring performance in your TresJS applications. This control displays real-time FPS information: ```vue [app.vue] ``` ::tip TresLeches automatically displays an FPS graph overlay when you use the `fpsgraph` control. This provides real-time performance insights without manual implementation. Learn more at [TresLeches Documentation](https://tresleches.tresjs.org/misc/#fpsgraph). :: ## Key Takeaways :::card-group ::card{title="Template Refs First" icon="i-lucide-target"} Use template refs for direct Three.js instance access in render loops to avoid reactivity overhead. :: ::card{title="Shallow Reactivity" icon="i-lucide-layers-2"} Use `shallowRef` and `shallowReactive` when you need some reactivity without deep proxy overhead. :: ::card{title="Separate Concerns" icon="i-lucide-git-branch"} Keep UI state reactive and animation state non-reactive for optimal performance. :: ::card{title="Monitor Performance" icon="i-lucide-activity"} Use Nuxt DevTools and `@tresjs/leches` performance monitoring to identify reactivity bottlenecks in your 3D scenes. :: ::: ::tip Remember: Vue's reactivity is powerful for UI updates but can be expensive in high-frequency render loops. Choose the right tool for each use case - reactive for user interactions, template refs for animations. :: By understanding and applying these reactivity patterns, you can create performant 3D experiences that leverage Vue's strengths while avoiding common performance pitfalls.