Performance: Computed in Composition API
This affects how efficiently reactive data updates trigger UI changes and how often expensive calculations run.
import { ref, computed } from 'vue'; const count = ref(0); const double = computed(() => count.value * 2);
import { ref } from 'vue'; const count = ref(0); function double() { return count.value * 2; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using plain functions for derived state | No direct DOM ops | Triggers recalculation on every access | Minimal paint but CPU heavy | [X] Bad |
| Using computed properties | No direct DOM ops | Recalculates only on dependency change | Minimal paint and CPU efficient | [OK] Good |
computed properties in Vue's Composition API?fullName inside setup()?setup():
const count = ref(2); const double = computed(() => count.value * 2); count.value = 5; console.log(double.value);What will be logged to the console?
setup():
const firstName = ref('John');
const lastName = ref('Doe');
const fullName = computed(() => {
firstName + ' ' + lastName
});statusMessage that returns "Active" if isActive is true, "Inactive" if false, and "Unknown" if isActive is null or undefined. Which code correctly implements this inside setup()?Pass the test and this pattern moves unknown