Performance: Plugin creation basics
Plugin creation affects initial app load time and runtime performance by adding extra code and possibly global reactive state.
import { createApp } from 'vue'; import App from './App.vue'; const app = createApp(App); // Lazy load plugin only when needed async function loadPlugin() { const { default: MyPlugin } = await import('./my-plugin'); app.use(MyPlugin); } loadPlugin(); app.mount('#app');
import { createApp } from 'vue'; import MyPlugin from './my-plugin'; const app = createApp(App); app.use(MyPlugin); app.mount('#app'); // MyPlugin registers many global components and heavy logic eagerly
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Eager plugin registration with many global components | High (many nodes registered) | Multiple reflows during mount | High paint cost due to many components | [X] Bad |
| Lazy-loaded plugin with selective global registration | Low (only needed nodes) | Single reflow on mount | Lower paint cost | [OK] Good |
const LoggerPlugin = {
install(app) {
app.config.globalProperties.$log = (msg) => console.log('Log:', msg)
}
}
const app = Vue.createApp({
mounted() {
this.$log('Hello')
}
})
app.use(LoggerPlugin)
app.mount('#app')const MyPlugin = {
install() {
console.log('Plugin installed')
}
}
const app = Vue.createApp({})
app.use(MyPlugin)
app.mount('#app')const MyPlugin = {
install(app) {
// Register component
app.component('MyButton', {
template: 'Click me'
})
// Add global method
app.config.globalProperties.$alert = (msg) => alert(msg)
}
}
const app = Vue.createApp({})
app.use(MyPlugin)
app.mount('#app')this.$alert('Hi') inside a component?Pass the test and this pattern moves unknown