---
title: Return Stable Object References from Computed Properties
impact: MEDIUM
impactDescription: Computed properties returning new objects trigger effects even when values haven't meaningfully changed
type: efficiency
tags: [vue3, computed, performance, reactivity, vue3.4]
---
# Return Stable Object References from Computed Properties
**Impact: MEDIUM** - In Vue 3.4+, computed properties only trigger effects when their value changes. However, if a computed returns a new object each time, Vue cannot detect that the values inside are the same. This causes unnecessary effect re-runs.
For primitive values, Vue 3.4+ handles this automatically. For objects, manually compare and return the previous value when nothing meaningful has changed.
## Task Checklist
- [ ] For computed properties returning primitives, Vue 3.4+ handles stability automatically
- [ ] For computed properties returning objects, compare with previous value and return old reference if unchanged
- [ ] Always perform the full computation before comparing (to track dependencies correctly)
- [ ] Consider if you really need to return an object, or if primitives would suffice
**Incorrect:**
```vue
```
**Correct:**
```vue
```
## Primitive vs Object Computed Behavior (Vue 3.4+)
```javascript
import { ref, computed, watchEffect } from 'vue'
const count = ref(0)
// PRIMITIVE: Vue automatically detects value hasn't changed
const isEven = computed(() => count.value % 2 === 0)
watchEffect(() => console.log(isEven.value)) // true
count.value = 2 // isEven still true - NO log
count.value = 4 // isEven still true - NO log
count.value = 3 // isEven now false - logs: false
// OBJECT: New reference every time (without manual comparison)
const obj = computed(() => ({ isEven: count.value % 2 === 0 }))
watchEffect(() => console.log(obj.value)) // { isEven: true }
count.value = 2 // Logs again! New object reference
count.value = 4 // Logs again! New object reference
```
## Advanced: Deep Object Comparison
```javascript
import { ref, computed } from 'vue'
import { isEqual } from 'lodash-es' // For deep comparison
const filters = ref({ category: 'all', sortBy: 'date', page: 1 })
// For complex objects, use deep comparison
const activeFilters = computed((oldValue) => {
const newValue = {
...filters.value,
hasFilters: filters.value.category !== 'all' || filters.value.sortBy !== 'date'
}
// Deep compare for complex objects
if (oldValue && isEqual(oldValue, newValue)) {
return oldValue
}
return newValue
})
```
## Important: Always Compute Before Comparing
```javascript
// BAD: Early return prevents dependency tracking
const optimized = computed((oldValue) => {
if (oldValue && someCondition) {
return oldValue // Dependencies not tracked!
}
return computeExpensiveValue()
})
// GOOD: Compute first, then compare
const optimized = computed((oldValue) => {
const newValue = computeExpensiveValue() // Always track dependencies
if (oldValue && newValue === oldValue) {
return oldValue
}
return newValue
})
```
## Reference
- [Vue.js Performance - Computed Stability](https://vuejs.org/guide/best-practices/performance.html#computed-stability)
- [Vue.js Computed Properties](https://vuejs.org/guide/essentials/computed.html)