Performance: Why reactivity is Vue's core concept
Reactivity affects how quickly the UI updates in response to data changes, impacting interaction responsiveness and visual stability.
import { ref, watch } from 'vue'; const count = ref(0); watch(count, (newVal) => { // Vue updates DOM reactively and efficiently });
const data = { count: 0 }; function updateUI() { document.getElementById('count').textContent = data.count; } // Manually call updateUI() after every data change
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual DOM updates on data change | Many direct DOM writes | Multiple reflows per update | High paint cost due to full updates | [X] Bad |
| Vue reactive refs with watch | Minimal DOM writes via reactive system | Single reflow per batch | Low paint cost with targeted updates | [OK] Good |
ref for single primitive values and reactive for objects.ref(0) creates a reactive number, while reactive expects an object.const state = reactive({ count: 0 });
state.count++;
console.log(state.count);reactive function wraps the object so changes to properties are tracked.state.count++ increases count to 1, so logging prints 1.const state = ref({ count: 0 });
state.count++;
console.log(state.count);ref wraps the entire object inside a .value property.state.count is wrong; it should be state.value.count.const data = ??? data.user.name = 'Alice';
reactive makes the whole object and nested properties reactive automatically.reactive, updating data.user.name triggers UI updates without extra refs.Pass the test and this pattern moves unknown