Bird
Raised Fist0
Vueframework~8 mins

Computed vs method performance in Vue - Performance Comparison

Choose your learning style10 modes available
Performance: Computed vs method performance
MEDIUM IMPACT
This affects how fast Vue updates and renders reactive data on the page during user interaction.
Displaying a reactive value that depends on other reactive data
Vue
export default {
  data() {
    return { count: 0 };
  },
  computed: {
    doubled() {
      return this.count * 2;
    }
  }
}
Computed caches the result and only recalculates when 'count' changes, reducing unnecessary work.
📈 Performance GainReduces recalculations to only when dependencies change, improving input responsiveness (INP).
Displaying a reactive value that depends on other reactive data
Vue
export default {
  data() {
    return { count: 0 };
  },
  methods: {
    doubled() {
      return this.count * 2;
    }
  }
}
The method runs every time the component re-renders, causing repeated calculations even if the data hasn't changed.
📉 Performance CostTriggers recalculation on every render, increasing CPU usage and slowing interaction responsiveness.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Method for reactive valueNo extra DOM nodesMultiple recalculations trigger more JS workNormal paint cost[X] Bad
Computed property for reactive valueNo extra DOM nodesRecalculates only on dependency changeNormal paint cost[OK] Good
Rendering Pipeline
Vue tracks reactive dependencies for computed properties, so it recalculates only when needed. Methods run fresh every render, causing more work in the rendering pipeline.
→JavaScript Execution
→Style Calculation
→Layout
→Paint
⚠️ BottleneckJavaScript Execution due to repeated calculations in methods
Core Web Vital Affected
INP
This affects how fast Vue updates and renders reactive data on the page during user interaction.
Optimization Tips
1Use computed properties for values derived from reactive data to cache results.
2Avoid methods for reactive display logic that runs on every render.
3Check performance in DevTools to see if recalculations are excessive.
Performance Quiz - 3 Questions
Test your performance knowledge
Which Vue feature caches reactive data to avoid unnecessary recalculations?
AMethods
BComputed properties
CWatchers
DDirect DOM manipulation
DevTools: Performance
How to check: Record a performance profile while interacting with the component. Look for repeated JavaScript function calls for methods vs fewer calls for computed properties.
What to look for: High CPU time spent in method functions indicates poor performance; fewer recalculations with computed show better efficiency.

Practice

(1/5)
1. Which statement best explains why computed properties are generally faster than methods in Vue?
easy
A. Computed properties cache their results and only recalculate when dependencies change.
B. Methods run only once and then cache their results automatically.
C. Computed properties run every time the component renders without caching.
D. Methods cache results and update only when data changes.

Solution

  1. Step 1: Understand computed property behavior

    Computed properties remember their last result and only recalculate if their dependent data changes.
  2. Step 2: Contrast with methods

    Methods run fresh every time they are called, without caching results.
  3. Final Answer:

    Computed properties cache their results and only recalculate when dependencies change. -> Option A
  4. Quick Check:

    Computed cache = B [OK]
Hint: Remember: computed caches, methods do not [OK]
Common Mistakes:
  • Thinking methods cache results automatically
  • Believing computed properties run every render
  • Confusing when caching happens
2. Which of the following is the correct syntax to define a computed property named fullName in a Vue 3 component using the Composition API?
easy
A. const fullName = () => firstName + ' ' + lastName;
B. methods: { fullName() { return firstName + ' ' + lastName } }
C. computed: { fullName() { return firstName + ' ' + lastName } }
D. const fullName = computed(() => firstName + ' ' + lastName);

Solution

  1. Step 1: Identify Composition API syntax

    In Vue 3 Composition API, computed properties are created by calling the computed function with a getter function.
  2. Step 2: Check each option

    const fullName = computed(() => firstName + ' ' + lastName); correctly uses computed(() => ...). Options B and C use Options API syntax, and D is just a plain function without caching.
  3. Final Answer:

    const fullName = computed(() => firstName + ' ' + lastName); -> Option D
  4. Quick Check:

    Composition API computed = A [OK]
Hint: Composition API computed uses computed(() => ...) [OK]
Common Mistakes:
  • Mixing Options API and Composition API syntax
  • Using methods syntax for computed
  • Not wrapping in computed() function
3. Given the Vue component below, what will be logged to the console when the button is clicked twice?
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 };
}
medium
A. Method called: 0\nMethod called: 0
B. Computed called\nMethod called: 2\nMethod called: 4
C. Computed called\nMethod called: 2\nComputed called\nMethod called: 4
D. Computed called\nComputed called\nMethod called: 2\nMethod called: 4

Solution

  1. Step 1: Understand computed caching and logging

    The computed property logs 'Computed called' only when recalculated. It recalculates when count.value changes.
  2. 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'.
  3. Final Answer:

    Computed called Method called: 2 Computed called Method called: 4 -> Option C
  4. Quick Check:

    Computed logs twice, method logs twice = A [OK]
Hint: Computed logs only on dependency change, methods run every call [OK]
Common Mistakes:
  • Assuming computed logs every method call
  • Ignoring reactive dependency triggers
  • Confusing method and computed logs
4. You have a Vue component where a method is used to calculate a value that depends on reactive data. The UI is slow because the method runs on every render. How can you fix this performance issue?
medium
A. Convert the method to a computed property so it caches results until dependencies change.
B. Wrap the method call inside a watch to run less often.
C. Use a lifecycle hook to call the method only once.
D. Move the method logic to a separate utility file.

Solution

  1. Step 1: Identify the cause of slowness

    Methods run fresh every render, causing repeated calculations and slowing UI.
  2. Step 2: Apply computed property for caching

    Computed properties cache results and only recalculate when reactive dependencies change, improving performance.
  3. Final Answer:

    Convert the method to a computed property so it caches results until dependencies change. -> Option A
  4. Quick Check:

    Use computed for caching = C [OK]
Hint: Switch slow methods to computed for caching [OK]
Common Mistakes:
  • Using watch incorrectly to reduce method calls
  • Thinking lifecycle hooks solve reactive recalculations
  • Moving logic without caching benefits
5. You have a Vue 3 component that displays a filtered list based on a search term. The filtering is done inside a method called 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.
hard
A. Use a method but memoize results manually with a cache object.
B. Replace the method with a computed property that returns the filtered list, so filtering runs only when list or search term changes.
C. Move filtering logic to a watcher that updates a reactive variable with filtered results.
D. Keep the method but debounce the search input to reduce calls.

Solution

  1. Step 1: Identify reactive dependencies

    The filtered list depends on the reactive list and search term, so it should update only when these change.
  2. Step 2: Use computed property for caching

    Computed properties cache results and recalculate only when dependencies change, making filtering efficient.
  3. Step 3: Compare other options

    Debouncing reduces calls but does not cache results. Watchers add complexity. Manual memoization is error-prone.
  4. 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 B
  5. Quick Check:

    Use computed for reactive filtering = D [OK]
Hint: Use computed to cache filtered results by dependencies [OK]
Common Mistakes:
  • Relying on debouncing without caching
  • Using watchers unnecessarily
  • Trying manual memoization instead of computed
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard