0
0
Vueframework~8 mins

h function for creating vnodes in Vue - Performance & Optimization

Choose your learning style9 modes available
Performance: h function for creating vnodes
MEDIUM IMPACT
This affects how Vue creates virtual DOM nodes, impacting rendering speed and memory usage during component updates.
Creating virtual DOM nodes in Vue components
Vue
import { h } from 'vue';

const items = ['Item 1', 'Item 2', 'Item 3'];

export default {
  render() {
    return h('div', {}, items.map(item => h('p', {}, item)));
  }
};
Using array mapping to generate vnodes reduces code repetition and allows Vue to optimize vnode diffing efficiently.
📈 Performance GainReduces CPU work by batching vnode creation, improving update speed and lowering interaction delay.
Creating virtual DOM nodes in Vue components
Vue
import { h } from 'vue';

export default {
  render() {
    return h('div', {}, [
      h('p', {}, 'Item 1'),
      h('p', {}, 'Item 2'),
      h('p', {}, 'Item 3')
    ]);
  }
};
Manually creating many vnode calls without abstraction causes repetitive vnode creation and can lead to more CPU work on updates.
📉 Performance CostTriggers multiple vnode creations and re-renders, increasing CPU usage and blocking rendering for tens of milliseconds on large lists.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Manual multiple h calls without abstractionMany vnode creationsMultiple reflows on updatesHigher paint cost due to frequent DOM changes[X] Bad
Using array map to batch vnode creationFewer vnode creations, batchedSingle reflow per update batchLower paint cost with minimal DOM changes[OK] Good
Rendering Pipeline
The h function creates virtual DOM nodes which Vue uses to compare with previous nodes. This diffing process determines minimal DOM updates.
Virtual DOM Creation
Diffing
DOM Updates
⚠️ BottleneckVirtual DOM Creation and Diffing when many vnodes are created inefficiently
Core Web Vital Affected
INP
This affects how Vue creates virtual DOM nodes, impacting rendering speed and memory usage during component updates.
Optimization Tips
1Batch vnode creation using arrays or loops to reduce CPU overhead.
2Avoid repetitive manual vnode calls for similar elements.
3Profile vnode creation to detect and optimize slow rendering.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using the h function efficiently in Vue?
ATriggers more reflows for better visuals
BIncreases bundle size significantly
CReduces CPU work by minimizing vnode creation
DBlocks rendering for longer to ensure accuracy
DevTools: Performance
How to check: Record a performance profile while interacting with the component, then inspect the scripting and rendering sections for vnode creation and DOM update times.
What to look for: Look for long scripting tasks related to vnode creation and multiple layout/repaint events indicating inefficient vnode handling.