Vue Computed Properties

Vue Tutorial

Vue Computed Properties

In Vue.js, computed properties are used to create derived (calculated) values based on reactive data.

They are cached, meaning they run only when their dependencies change.


 What are Computed Properties?

Computed properties are functions that behave like data properties.

  •  Automatically update when dependent data changes
  •  Cached for better performance
  • Ideal for calculations, filtering, formatting

 Basic Syntax


 Example-1: Simple Computed Property

  • fullName updates automatically when firstName or lastName changes.

 Example-2: Computed vs Method

  •  Runs only when price or quantity changes
  • Method would run on every re-render

 Example-3: Filtering with Computed

  •  Best practice instead of v-if inside v-for

 Example-4: Computed with Getter & Setter

  •  Enables two-way computed logic

 Computed vs Methods (Important)

FeatureComputedMethods
CachedYes No
ReactiveYesYes
Best forDerived dataActions / events
PerformanceHighLower
  •  Use computed for calculations
  •  Use methods for event handling

 Computed vs Watchers

FeatureComputedWatch
PurposeDerive valueReact to change
Return valueYesNo
Side effectsNoYes
  •  Computed → data transformation
  •  Watch → API calls, logging

 Common Use Cases

  • Full name generation

  • Price calculations

  • Filtering lists

  • Sorting data

  • Conditional classes

  • Validation logic


 Common Mistakes

  •  Doing API calls in computed
  •  Mutating data inside computed
  •  Using methods instead of computed

 Best Practices

  •  Keep computed pure (no side effects)
  • Use meaningful names
  • Prefer computed over methods for derived data
  • Use setter only when needed

 Summary Table

Use CaseUse
Calculated valueComputed
Filtering listsComputed
Formatting outputComputed
Click handlingMethods
Side effectsWatchers

 Conclusion

Computed properties are one of the most powerful features of Vue.
They help you write clean, fast, and maintainable code by separating logic from templates.

You may also like...