Performance: How Vue tracks dependencies automatically
This affects how efficiently Vue updates the UI by tracking which parts of the data each component uses, minimizing unnecessary work.
const state = reactive({ count: 0, name: 'Vue' }); watchEffect(() => { console.log(state.count); }); // Vue tracks only 'count' dependency, ignoring 'name' changes.
const state = reactive({ count: 0, name: 'Vue' }); watchEffect(() => { console.log(state.count, state.name); }); // This logs whenever any reactive property changes, even if only one is needed.
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Tracking all reactive properties regardless of usage | High (many updates) | Multiple per unrelated change | High (many repaints) | [X] Bad |
| Tracking only accessed reactive properties automatically | Low (targeted updates) | Single per relevant change | Low (minimal repaints) | [OK] Good |
ref and computed to track dependencies automatically.ref and computed. -> Option Dref and computed. [OK]ref is used to create a reactive reference to a primitive value like a number.reactive is for objects, computed creates derived values, and watch observes changes but does not create reactive data.const count = ref(0); const double = computed(() => count.value * 2); console.log(double.value); count.value = 5; console.log(double.value);
count.value is 0, so double.value is 0 * 2 = 0.count.value changes to 5, double automatically recalculates to 5 * 2 = 10.const count = ref(0); const double = computed(() => count * 2); console.log(double.value);
.value property, so you must use count.value to get the number.count directly, which is a ref object, not the number, causing wrong behavior.ref or reactive can be used inside a computed property to automatically track dependencies.computed property that filters the reactive items array and returns the count of items with price > 10. -> Option Acomputed property that filters the reactive items array and returns the count of items with price > 10. [OK]Pass the test and this pattern moves unknown