0
0
Vueframework~8 mins

Computed with getter and setter in Vue - Performance & Optimization

Choose your learning style9 modes available
Performance: Computed with getter and setter
MEDIUM IMPACT
This affects how reactive data updates trigger DOM updates and re-renders in Vue components.
Managing reactive state with computed properties that need both read and write access
Vue
const fullName = computed({
  get() {
    return firstName.value + ' ' + lastName.value;
  },
  set(value) {
    const parts = value.split(' ');
    firstName.value = parts[0];
    lastName.value = parts.slice(1).join(' ') || '';
  }
});
The setter allows direct updates to the computed property, batching reactive changes and reducing redundant updates.
📈 Performance Gainreduces reactive update cycles and re-renders by combining reads and writes in one reactive property
Managing reactive state with computed properties that need both read and write access
Vue
const fullName = computed(() => firstName.value + ' ' + lastName.value);
// Updating fullName directly is not possible, so manual watchers or methods are used to sync changes
Without a setter, updating the computed property requires extra watchers or methods, causing more reactive updates and potential redundant re-renders.
📉 Performance Costtriggers multiple reactive updates and re-renders for a single logical change
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Computed with getter only and manual syncMultiple reactive triggersMultiple reflows per updateHigher paint cost due to redundant renders[X] Bad
Computed with getter and setterSingle reactive trigger per updateSingle reflow per updateLower paint cost due to optimized renders[OK] Good
Rendering Pipeline
Vue tracks dependencies during the computed property's getter execution. When dependencies change, Vue schedules updates. The setter allows direct mutation of dependencies, minimizing extra watchers and reactivity overhead.
Dependency Tracking
Reactive Update Scheduling
Component Re-render
⚠️ BottleneckReactive Update Scheduling when multiple watchers or manual syncs are used instead of a setter
Core Web Vital Affected
INP
This affects how reactive data updates trigger DOM updates and re-renders in Vue components.
Optimization Tips
1Use computed properties with both getter and setter to optimize reactive updates.
2Avoid manual syncing of computed properties without setters to prevent redundant re-renders.
3Monitor reactive update cycles in DevTools Performance panel to catch inefficiencies.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using a computed property with both getter and setter in Vue?
AIt increases the number of watchers for better tracking.
BIt reduces redundant reactive updates by combining read and write logic.
CIt delays DOM updates to improve load time.
DIt disables reactivity for faster rendering.
DevTools: Performance
How to check: Record a performance profile while interacting with the component updating computed properties. Look for multiple reactive update cycles or redundant component re-renders.
What to look for: Fewer reactive update events and shorter scripting time indicate better performance with getter/setter computed properties.