0
0
Vueframework~10 mins

Computed vs method performance in Vue - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Computed vs method performance
Component Renders
Access Computed Property
Check Cache
Display Value
Access Method
Run Method Function
Return Result
Display Value
When the component renders, accessing a computed property first checks if the cached value exists to reuse it, while calling a method runs the function every time without caching.
Execution Sample
Vue
const count = ref(0);
const doubleComputed = computed(() => count.value * 2);
function doubleMethod() { return count.value * 2; }

// Template uses {{ doubleComputed }} and {{ doubleMethod() }}
Shows a reactive count with a computed property and a method both doubling the count, used in the template.
Execution Table
StepActioncount.valueComputed CacheMethod CalledOutput ComputedOutput Method
1Initial render, count=000NoCalculates 0*2=0 and cachesN/A
2Template accesses computed00NoReturns cached 0N/A
3Template calls method00YesN/ACalculates 0*2=0
4count.value increments to 11invalidatedNoN/AN/A
5Template accesses computed12NoCalculates 1*2=2 and cachesN/A
6Template calls method12YesN/ACalculates 1*2=2
7count.value increments to 22invalidatedNoN/AN/A
8Template accesses computed24NoCalculates 2*2=4 and cachesN/A
9Template calls method24YesN/ACalculates 2*2=4
10End of trace24Yes44
💡 Execution stops after showing how computed caches and method runs on each render and reactive change.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
count.value0001122
Computed Cacheempty00invalidated2invalidated4
Method CalledNoNoYesNoYesNoYes
Output ComputedN/A002244
Output MethodN/AN/A0N/A2N/A4
Key Moments - 3 Insights
Why does the computed property not recalculate every time the template accesses it?
Because computed properties cache their result until their reactive dependencies change, as shown in execution_table rows 2 and 5 where the cached value is reused.
Why does the method run every time it is called in the template?
Methods are regular functions and do not cache results, so they run fresh on every call, as seen in execution_table rows 3, 6, and 9.
What causes the computed cache to invalidate and recalculate?
When a reactive dependency like count.value changes, the computed cache invalidates and recalculates on next access, shown in execution_table rows 4 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 5. What is the computed cache value after recalculation?
A0
B2
Cempty
D4
💡 Hint
Check the 'Computed Cache' column at step 5 in the execution_table.
At which step does the method first run after count.value changes to 1?
AStep 6
BStep 5
CStep 4
DStep 7
💡 Hint
Look for 'Method Called' marked 'Yes' after count.value changes to 1 in execution_table.
If count.value never changes, how would the computed cache behave?
AIt never caches
BIt recalculates every time
CIt caches once and reuses forever
DIt clears cache every render
💡 Hint
Refer to the caching behavior shown in execution_table steps 1 and 2.
Concept Snapshot
Computed properties cache their results and only recalculate when dependencies change.
Methods run fresh every time they are called.
Use computed for expensive or repeated calculations.
Use methods for simple or event-driven calculations.
Computed improves performance by avoiding unnecessary recalculations.
Full Transcript
This visual trace shows how Vue's computed properties cache their results and only recalculate when reactive dependencies like count.value change. Methods, in contrast, run their function every time they are called in the template without caching. The execution table tracks count.value changes, computed cache state, method calls, and outputs step-by-step. Key moments clarify why computed properties improve performance by caching and when caches invalidate. The quiz tests understanding of cache values and method calls referencing the execution table. This helps beginners see the difference in behavior and performance between computed properties and methods in Vue.