0
0
Vueframework~8 mins

Provide and inject for deep passing in Vue - Performance & Optimization

Choose your learning style9 modes available
Performance: Provide and inject for deep passing
MEDIUM IMPACT
This affects how data is passed deeply through component trees without prop drilling, impacting rendering and reactivity performance.
Passing data deeply through many nested components
Vue
const Parent = { provide() { return { value: this.value } }, data() { return { value: 0 } }, template: `<Child1 />` }
const Child1 = { inject: ['value'], template: `<Child2 />` }
const Child2 = { inject: ['value'], template: `<Child3 />` }
const Child3 = { inject: ['value'], template: `<div>{{ value }}</div>` }
Using provide/inject avoids prop drilling and reduces unnecessary re-renders on intermediate components.
📈 Performance GainSingle reactive source with fewer re-renders and cleaner component code
Passing data deeply through many nested components
Vue
const Parent = { template: `<Child1 :value=\"value\" />`, data() { return { value: 0 } } }
const Child1 = { props: ['value'], template: `<Child2 :value=\"value\" />` }
const Child2 = { props: ['value'], template: `<Child3 :value=\"value\" />` }
const Child3 = { props: ['value'], template: `<div>{{ value }}</div>` }
Passing props through many layers causes repeated re-renders and verbose code, increasing DOM updates and CPU work.
📉 Performance CostTriggers multiple re-renders and prop updates for each intermediate component
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Prop drilling through many componentsMany prop updatesMultiple reflowsHigher paint cost[X] Bad
Provide/inject for deep passingSingle reactive sourceMinimal reflowsLower paint cost[OK] Good
Rendering Pipeline
Provide/inject passes reactive references through the component tree without intermediate prop updates, reducing layout recalculations and paint triggers.
Reactivity Tracking
Component Render
Layout
Paint
⚠️ BottleneckExcessive reactive updates if injected values change frequently
Core Web Vital Affected
INP
This affects how data is passed deeply through component trees without prop drilling, impacting rendering and reactivity performance.
Optimization Tips
1Avoid prop drilling by using provide/inject for deeply nested data passing.
2Minimize reactive updates on injected values to reduce layout recalculations.
3Use readonly or shallow reactive injections to optimize reactivity performance.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance benefit of using provide/inject instead of prop drilling in Vue?
AReduces unnecessary re-renders of intermediate components
BIncreases bundle size significantly
CTriggers more layout recalculations
DBlocks rendering until all props are passed
DevTools: Performance
How to check: Record a performance profile while interacting with components that receive injected data. Look for fewer component render events and less layout thrashing.
What to look for: Lower number of component updates and shorter scripting time indicate better performance with provide/inject.