Performance: JSX in Vue components
Using JSX in Vue components affects the rendering speed and bundle size due to how templates are compiled and how the virtual DOM updates.
import { defineComponent } from 'vue' export default defineComponent({ render() { return <div>{this.title} {this.subtitle}</div> } })
export default { render() { return h('div', this.title + ' ' + this.subtitle) } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual render function with h() | Moderate | Multiple per update | Medium | [!] OK |
| JSX in Vue components | Moderate | Few to moderate | Medium | [OK] Good |
| Vue SFC templates | Minimal | Minimal | Low | [OK] Good |
setup() function in Vue 3?setup() returns a render function, usually an arrow function returning JSX wrapped in parentheses.import { defineComponent } from 'vue';
export default defineComponent({
setup() {
const name = 'Vue';
return () => (
Hello, {name}!
);
}
});import { defineComponent } from 'vue';
export default defineComponent({
setup() {
const count = 0;
return () => {
{count}
};
}
});() => ( <button>{count}</button> ).setup()?map() returns an array of JSX elements, perfect for rendering lists.map() to return forEach which returns undefined. return () => (Pass the test and this pattern moves unknown