0
0
Vueframework~20 mins

Computed vs method performance in Vue - Practice Questions

Choose your learning style9 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.