0
0
Vueframework~8 mins

Reactive data with ref in Vue - Performance & Optimization

Choose your learning style9 modes available
Performance: Reactive data with ref
MEDIUM IMPACT
This affects how efficiently Vue tracks and updates changes in reactive data, impacting rendering speed and responsiveness.
Managing a single reactive primitive value in Vue
Vue
import { ref } from 'vue';
const count = ref(0);

function increment() {
  count.value++;
}
ref is optimized for single primitive values, creating a simple reactive wrapper that triggers updates only when the value changes.
📈 Performance Gainreduces reactivity overhead and improves update speed
Managing a single reactive primitive value in Vue
Vue
const count = reactive({ value: 0 });

function increment() {
  count.value++;
}
Using reactive for a single primitive adds unnecessary proxy overhead and can cause more complex reactivity tracking.
📉 Performance Costadds extra proxy layers and triggers more dependency tracking than needed
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Using reactive for primitiveMore proxy layers, deeper trackingPotentially more reflows if updates cascadeHigher paint cost due to extra updates[X] Bad
Using ref for primitiveMinimal proxy, direct trackingSingle reflow per updateLower paint cost with targeted updates[OK] Good
Rendering Pipeline
When a ref's value changes, Vue's reactivity system schedules component updates. The browser then recalculates styles and layouts only for affected components, minimizing work.
JavaScript Execution
Style Calculation
Layout
Paint
⚠️ BottleneckJavaScript Execution due to reactive dependency tracking
Core Web Vital Affected
INP
This affects how efficiently Vue tracks and updates changes in reactive data, impacting rendering speed and responsiveness.
Optimization Tips
1Use ref for single primitive reactive values to reduce overhead.
2Avoid using reactive for primitives to prevent unnecessary deep proxying.
3Changing a ref value triggers targeted component updates, improving responsiveness.
Performance Quiz - 3 Questions
Test your performance knowledge
Why is using ref better than reactive for a single primitive value in Vue?
ABecause ref creates a simpler reactive wrapper with less overhead
BBecause reactive does not track changes at all
CBecause ref disables reactivity for primitives
DBecause reactive only works with objects
DevTools: Performance
How to check: Record a performance profile while interacting with the component using ref. Look for minimal scripting time and fewer layout recalculations.
What to look for: Low JavaScript execution time and fewer layout thrashing events indicate good reactive data usage.