Bird
Raised Fist0
Vueframework~20 mins

Computed vs method performance in Vue - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Vue Computed vs Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
Difference in output when using computed vs method

Consider a Vue 3 component that displays a number doubled. Which option shows the correct output when using a computed property versus a method?

Vue
<script setup>
import { ref, computed } from 'vue'
const count = ref(2)
const doubleComputed = computed(() => count.value * 2)
function doubleMethod() {
  return count.value * 2
}
</script>
<template>
  <div>
    <p>Computed: {{ doubleComputed }}</p>
    <p>Method: {{ doubleMethod() }}</p>
  </div>
</template>
AComputed: 4, Method: 2
BComputed: 2, Method: 2
CComputed: 2, Method: 4
DComputed: 4, Method: 4
Attempts:
2 left
💡 Hint

Both computed properties and methods can return values, but computed caches results until dependencies change.

❓ state_output
intermediate
2:00remaining
Performance difference when updating reactive state

In Vue 3, what happens to the output when count changes rapidly and you use a computed property versus a method to double it?

Vue
<script setup>
import { ref, computed } from 'vue'
const count = ref(1)
const doubleComputed = computed(() => count.value * 2)
function doubleMethod() {
  return count.value * 2
}
// Imagine count.value changes rapidly
</script>
<template>
  <div>
    <p>Computed: {{ doubleComputed }}</p>
    <p>Method: {{ doubleMethod() }}</p>
  </div>
</template>
AComputed updates only when count changes; method recalculates every render
BComputed recalculates every render; method caches result
CBoth computed and method cache results until count changes
DNeither computed nor method update when count changes
Attempts:
2 left
💡 Hint

Think about caching behavior of computed properties versus methods.

📝 Syntax
advanced
2:00remaining
Correct syntax for defining a computed property in Vue 3

Which option shows the correct way to define a computed property that returns the uppercase version of a reactive name?

Vue
import { ref, computed } from 'vue'
const name = ref('vue')
// Define computed property here
Aconst upperName = computed { return name.value.toUpperCase() }
Bconst upperName = computed(() => name.value.toUpperCase())
Cconst upperName = computed(() => { name.value.toUpperCase() })
Dconst upperName = computed(() => name.toUpperCase())
Attempts:
2 left
💡 Hint

Remember computed takes a function returning a value.

🔧 Debug
advanced
2:00remaining
Why does this method cause unnecessary recalculations?

Given this Vue 3 component snippet, why does the method cause performance issues compared to a computed property?

Vue
<script setup>
import { ref } from 'vue'
const count = ref(0)
function double() {
  console.log('Calculating double')
  return count.value * 2
}
</script>
<template>
  <button @click="count++">Increment</button>
  <p>Double: {{ double() }}</p>
</template>
AThe method runs on every render causing repeated calculations and console logs
BThe method caches the result and never updates when count changes
CThe method causes a syntax error because it lacks a return statement
DThe method does not have access to count.value causing undefined errors
Attempts:
2 left
💡 Hint

Think about when methods run in Vue templates.

🧠 Conceptual
expert
3:00remaining
Choosing between computed and method for expensive calculations

You have a Vue 3 component that performs an expensive calculation based on reactive data. Which approach is best to optimize performance and why?

AUse a regular function outside the component to avoid reactivity overhead
BUse a method to recalculate the value every time the component renders for accuracy
CUse a computed property to cache the result and recalculate only when dependencies change
DUse a watcher to perform the calculation and store the result in a ref manually
Attempts:
2 left
💡 Hint

Consider caching and reactive updates.

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