Computed properties and methods both help show data in Vue. But computed properties remember their result until data changes, making them faster for repeated use.
Computed vs method performance in Vue
export default { data() { return { number: 5 }; }, computed: { squared() { return this.number * this.number; } }, methods: { squaredMethod() { return this.number * this.number; } } };
Computed properties are defined inside the computed object.
Methods are defined inside the methods object and run every time they are called.
value changes.computed: {
doubled() {
return this.value * 2;
}
}value did not change.methods: {
doubledMethod() {
return this.value * 2;
}
}This Vue component shows a number and its square calculated two ways: with a computed property and a method. Clicking the button increases the number.
Open your browser console to see when each calculation runs. The computed property runs only when the number changes. The method runs every time the template updates.
<template>
<div>
<p>Number: {{ number }}</p>
<p>Computed squared: {{ squared }}</p>
<p>Method squared: {{ squaredMethod() }}</p>
<button @click="increment">Increase Number</button>
</div>
</template>
<script setup>
import { ref, computed } from 'vue';
const number = ref(5);
function increment() {
number.value++;
}
const squared = computed(() => {
console.log('Computed runs');
return number.value * number.value;
});
function squaredMethod() {
console.log('Method runs');
return number.value * number.value;
}
</script>Computed properties cache their results and only update when their dependencies change.
Methods run fresh every time they are called, which can slow down your app if used for heavy calculations.
Use computed properties for values that depend on reactive data and need to update efficiently.
Computed properties are faster because they remember results until data changes.
Methods run every time and do not cache results.
Choose computed properties for reactive, dependent values to improve performance.
Practice
Solution
Step 1: Understand computed property behavior
Computed properties remember their last result and only recalculate if their dependent data changes.Step 2: Contrast with methods
Methods run fresh every time they are called, without caching results.Final Answer:
Computed properties cache their results and only recalculate when dependencies change. -> Option AQuick Check:
Computed cache = B [OK]
- Thinking methods cache results automatically
- Believing computed properties run every render
- Confusing when caching happens
fullName in a Vue 3 component using the Composition API?Solution
Step 1: Identify Composition API syntax
In Vue 3 Composition API, computed properties are created by calling thecomputedfunction with a getter function.Step 2: Check each option
const fullName = computed(() => firstName + ' ' + lastName); correctly usescomputed(() => ...). Options B and C use Options API syntax, and D is just a plain function without caching.Final Answer:
const fullName = computed(() => firstName + ' ' + lastName); -> Option DQuick Check:
Composition API computed = A [OK]
- Mixing Options API and Composition API syntax
- Using methods syntax for computed
- Not wrapping in computed() function
setup() {
const count = ref(0);
const doubleCount = computed(() => {
console.log('Computed called');
return count.value * 2;
});
function increment() {
count.value++;
console.log('Method called:', doubleCount.value);
}
return { count, doubleCount, increment };
}Solution
Step 1: Understand computed caching and logging
The computed property logs 'Computed called' only when recalculated. It recalculates whencount.valuechanges.Step 2: Trace the two button clicks
First click: count=1 triggers computed recalculation (logs 'Computed called'), then method logs 'Method called: 2'. Second click: count=2 triggers computed recalculation again (logs 'Computed called'), then method logs 'Method called: 4'.Final Answer:
Computed called Method called: 2 Computed called Method called: 4 -> Option CQuick Check:
Computed logs twice, method logs twice = A [OK]
- Assuming computed logs every method call
- Ignoring reactive dependency triggers
- Confusing method and computed logs
Solution
Step 1: Identify the cause of slowness
Methods run fresh every render, causing repeated calculations and slowing UI.Step 2: Apply computed property for caching
Computed properties cache results and only recalculate when reactive dependencies change, improving performance.Final Answer:
Convert the method to a computed property so it caches results until dependencies change. -> Option AQuick Check:
Use computed for caching = C [OK]
- Using watch incorrectly to reduce method calls
- Thinking lifecycle hooks solve reactive recalculations
- Moving logic without caching benefits
filteredList() which runs on every render, causing lag with large data. How can you optimize this using computed properties? Consider that the list and search term are reactive refs.Solution
Step 1: Identify reactive dependencies
The filtered list depends on the reactive list and search term, so it should update only when these change.Step 2: Use computed property for caching
Computed properties cache results and recalculate only when dependencies change, making filtering efficient.Step 3: Compare other options
Debouncing reduces calls but does not cache results. Watchers add complexity. Manual memoization is error-prone.Final Answer:
Replace the method with a computed property that returns the filtered list, so filtering runs only when list or search term changes. -> Option BQuick Check:
Use computed for reactive filtering = D [OK]
- Relying on debouncing without caching
- Using watchers unnecessarily
- Trying manual memoization instead of computed
Ready to prove it?
Pass the test and this pattern moves unknown
