Performance: Getters for computed store values
This affects how quickly the UI updates when store data changes and how much CPU is used during reactive computations.
const store = reactive({ items: [1, 2, 3] }); const doubled = computed(() => store.items.map(x => x * 2)); // cached until items change
const store = reactive({ items: [1, 2, 3] }); const doubled = () => store.items.map(x => x * 2); // recalculates on every access
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Non-computed getter function | Minimal | None directly | Low but CPU-heavy | [X] Bad |
| Computed getter with caching | Minimal | None directly | Low and efficient | [OK] Good |
completedTasks in a Vue store?state and returning a value. Arrow function with implicit return is common.state parameter and returns filtered tasks. getters: { completedTasks() { return this.tasks.filter(t => t.done) } } incorrectly uses this which is not available. getters: { completedTasks: function() { return state.tasks.filter(t => t.done) } } lacks state parameter. getters: { completedTasks: (state) => { state.tasks.filter(t => t.done) } } misses return statement.this inside getters instead of statestate as a parameterstate: { items: [ { id: 1, done: true }, { id: 2, done: false } ] }getters: { doneCount: (state) => state.items.filter(i => i.done).length }store.getters.doneCount return?done: true and one with done: false.done is true, so only one item matches. Then it returns the length, which is 1.getters: { activeUsers: (state) => { state.users.filter(u => u.active) } }return to output a value. Without it, the function returns undefined.state: { cart: [ { price: 10, quantity: 2 }, { price: 5 }, { price: 3, quantity: 4 } ] }Pass the test and this pattern moves unknown