Performance: Composable accepting parameters
MEDIUM IMPACT
This affects how efficiently Vue manages reactive state and re-renders components when composables receive parameters.
import { ref } from 'vue'; export function useCounter(initial) { const count = ref(initial); // No watch on parameter, parameter used only once return { count }; }
import { ref, watch } from 'vue'; export function useCounter(initial) { const count = ref(initial); watch(() => initial, (newVal) => { count.value = newVal; }); return { count }; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Watching parameter changes inside composable | Multiple reactive updates | Multiple reflows on parameter change | Higher paint cost due to frequent updates | [X] Bad |
| Using parameter only once as initial value | Single reactive update | Single reflow on state change | Lower paint cost with fewer updates | [OK] Good |