Discover how a small change can make your app feel lightning fast!
Computed vs method performance in Vue - When to Use Which
Imagine you have a webpage showing a list of items with prices, and you want to display the total price. You write a method to calculate the total every time the page updates.
Calling the method repeatedly makes your page slow because it recalculates the total even when the prices haven't changed. This wastes time and makes the app feel laggy.
Using computed properties, Vue automatically caches the total and only recalculates it when the prices actually change. This keeps your app fast and smooth without extra work.
methods: { total() { return this.items.reduce((sum, i) => sum + i.price, 0); } }computed: { total() { return this.items.reduce((sum, i) => sum + i.price, 0); } }It enables efficient updates by recalculating values only when needed, making your app faster and more responsive.
Think of a shopping cart that updates the total price instantly but only recalculates when you add or remove items, not every time you click elsewhere.
Methods recalculate on every call, which can slow down your app.
Computed properties cache results and update only when dependencies change.
Using computed properties improves performance and user experience.
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
