Performance: Dependency injection patterns
MEDIUM IMPACT
This affects the initial load time and runtime responsiveness by controlling how dependencies are created and shared in Vue components.
import { provide, inject, reactive } from 'vue'; // In a parent or root component export default { setup() { const service = reactive({ count: 0 }); provide('service', service); return {}; } } // In child components export default { setup() { const service = inject('service'); return { service }; } }
import { reactive } from 'vue'; export default { setup() { const service = reactive({ count: 0 }); // Each component creates its own service instance return { service }; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Creating new service instance per component | None | 0 | Low but repeated JS execution | [X] Bad |
| Using provide/inject to share service instance | None | 0 | Low JS execution once | [OK] Good |