0
0
Vueframework~8 mins

Sharing composables across components in Vue - Performance & Optimization

Choose your learning style9 modes available
Performance: Sharing composables across components
MEDIUM IMPACT
This affects how efficiently state and logic are reused across Vue components, impacting rendering speed and memory usage.
Sharing reactive state logic between multiple components
Vue
const sharedCount = ref(0);
function useCounter() {
  function increment() {
    sharedCount.value++;
  }
  return { count: sharedCount, increment };
}

// In each component
const { count, increment } = useCounter();
All components share the same reactive state, reducing memory and syncing updates efficiently.
📈 Performance GainSaves memory by sharing one reactive object; reduces redundant re-renders.
Sharing reactive state logic between multiple components
Vue
function useCounter() {
  const count = ref(0);
  function increment() {
    count.value++;
  }
  return { count, increment };
}

// In each component
const { count, increment } = useCounter();
Each component creates its own separate reactive state, causing duplicated memory and no shared updates.
📉 Performance CostIncreases memory usage linearly with components; triggers independent reactivity and re-renders.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Separate reactive state per componentMultiple reactive refs per componentMultiple re-renders on updatesHigher paint cost due to many updates[X] Bad
Shared reactive state via composableSingle reactive ref sharedSingle re-render per updateLower paint cost with fewer updates[OK] Good
Rendering Pipeline
When composables are shared properly, Vue reactivity tracks a single source of truth, minimizing redundant reactive updates and re-renders.
Reactive Dependency Tracking
Component Render
DOM Update
⚠️ BottleneckExcessive reactive state duplication causes more dependency tracking and component re-renders.
Core Web Vital Affected
INP
This affects how efficiently state and logic are reused across Vue components, impacting rendering speed and memory usage.
Optimization Tips
1Avoid creating new reactive state inside composables for each component if shared state is intended.
2Use shared refs or stores to centralize reactive data across components.
3Monitor component re-renders in DevTools to detect unnecessary reactive duplication.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance issue when each component creates its own reactive state from a composable?
ACSS styles are recalculated unnecessarily
BThe app bundle size grows significantly
CMemory usage increases and components re-render independently
DThe browser blocks rendering on network requests
DevTools: Performance
How to check: Record a performance profile while interacting with components using the composable. Look for repeated reactive updates and component re-renders.
What to look for: High number of component renders or reactive effect recalculations indicates duplicated state; fewer renders means better sharing.