Performance: Why advanced reactivity matters
This affects how fast the page updates when data changes, impacting user interaction smoothness and rendering speed.
const count = ref(0); const items = ref([]); watch(count, () => { // only runs when count changes efficientUpdate(); });
const state = reactive({ count: 0, items: [] }); watch(state, () => { // runs on any change, even unrelated expensiveUpdate(); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Watching entire reactive object | High (many nodes affected) | Multiple reflows per unrelated change | High paint cost due to frequent updates | [X] Bad |
| Watching specific refs or computed | Low (only affected nodes) | Single reflow per relevant change | Low paint cost with minimal updates | [OK] Good |
ref to create reactive primitive values like numbers.reactive is for objects, computed for derived values, watch for side effects.import { ref, watch } from 'vue';
const count = ref(0);
watch(count, (newVal) => {
console.log('Count changed to', newVal);
});
count.value = 5;
count.value = 10;import { reactive } from 'vue';
const state = reactive({ count: 0 });
function increment() {
state.count++;
}
increment();
console.log(state.count);reactive creates a reactive object whose properties can be accessed and mutated directly.state.count directly is valid and updates the reactive state.import { ref, computed } from 'vue';
const count = ref(3);
const doubleCount = ???
console.log(doubleCount.value);computed(() => count.value * 2) to create a reactive doubleCount.Pass the test and this pattern moves unknown