0
0
Vueframework~8 mins

Higher-order components concept in Vue - Performance & Optimization

Choose your learning style9 modes available
Performance: Higher-order components concept
MEDIUM IMPACT
This concept affects rendering speed and responsiveness by adding extra component layers in the Vue component tree.
Wrapping a base component with a higher-order component (HOC) to add behavior
Vue
const useLogger = (props) => {
  watchEffect(() => {
    console.log('Rendering component with props:', props);
  });
};

const Button = defineComponent({
  props: { label: String },
  setup(props) {
    useLogger(props);
    return () => h('button', null, props.label);
  }
});

// Usage in template
// <Button label="Click me" />
Using a composable function inside the component avoids extra wrapper components, reducing component tree depth and reactive overhead.
📈 Performance GainRemoves extra component layer, reducing reactive tracking and improving interaction responsiveness.
Wrapping a base component with a higher-order component (HOC) to add behavior
Vue
const withLogger = (WrappedComponent) => {
  return {
    props: WrappedComponent.props,
    setup(props, { slots }) {
      console.log('Rendering component with props:', props);
      return () => h(WrappedComponent, props, slots.default ? slots.default() : undefined);
    }
  };
};

const EnhancedButton = withLogger(Button);

// Usage in template
// <EnhancedButton label="Click me" />
This HOC creates a new component wrapper on each render, adding extra layers and causing more Vue reactivity tracking and rendering overhead.
📉 Performance CostAdds 1 extra component layer per use, increasing DOM depth and reactive updates, causing slower INP.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
HOC wrapping base componentIncreases component nodes by 1 per useTriggers reactive updates on wrapper and baseSlightly higher due to deeper tree[!] OK
Composable function inside componentNo extra component nodesReactive updates only on base componentLower paint cost due to simpler tree[OK] Good
Rendering Pipeline
Higher-order components add extra component nodes in the Vue virtual DOM tree, increasing the work during the render and patch phases.
Virtual DOM Render
Reactive Tracking
Component Patch
⚠️ BottleneckExtra component layers increase reactive dependency tracking and virtual DOM diffing cost.
Core Web Vital Affected
INP
This concept affects rendering speed and responsiveness by adding extra component layers in the Vue component tree.
Optimization Tips
1Avoid unnecessary higher-order components to keep the component tree shallow.
2Use composable functions to share logic without adding extra component wrappers.
3Monitor component tree depth in DevTools to spot performance bottlenecks.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance drawback of using higher-order components in Vue?
AThey reduce bundle size significantly
BThey eliminate the need for reactive tracking
CThey add extra component layers increasing render and reactive overhead
DThey improve Largest Contentful Paint (LCP)
DevTools: Performance
How to check: Record a performance profile while interacting with the component. Look for extra component render calls and reactive effect executions.
What to look for: Check the flame chart for extra component layers and longer render times indicating overhead from HOCs.