About 468 wordsAbout 2 min
Vue3Performance Optimization
2025-01-18
Optimization of Reactive System
Avoid Unnecessary Reactive Data
Use computed and Avoid Unnecessary watch
: Try to avoid unnecessary operations in watch, and avoid triggering watch execution immediately during initialization. watchEffect will execute immediatelySelective Access to Reactive Objects
: When you need to get specific properties from a reactive object, try to manually destructure the properties instead of passing the entire object. This can avoid unnecessary dependency tracking.
Component Rendering Optimization
Avoid Unnecessary Rendering
- Use v-show instead of v-if
- Use key to optimize v-for rendering
Avoid Frequent Re-rendering of Components
- v-memo: Use v-memo to cache the rendering result of components or elements, avoiding re-rendering when dependencies do not change.
- Manually control re-rendering: Control whether the component needs to be updated through watch, computed, or the lazy feature of ref, avoiding redundant re-rendering.
- v-once: If a part of the view only needs to be rendered once, or some data does not change frequently, you can use v-once to render that part of the view, avoiding re-rendering during each update.
Split Large Components
For larger components, load them on demand to avoid loading too much content during initial rendering.<KeepAlive> and Dynamic Components
Use<keep-alive>
to cache component states on dynamically loaded pages or components, avoiding repeated rendering. This is especially useful for caching the state of some pages when switching routes.
Asynchronous Loading and Code Splitting
Lazy Loading Routes
const routes = [ { path: '/about', component: () => import('./views/About.vue'), }, ];
Lazy Loading Components
For infrequently used components, delay loading them through defineAsyncComponent, loading the components only when needed.const AsyncComponent = defineAsyncComponent(() => import('./components/AsyncComponent.vue'));
State Management Optimization
Local State Management
Use modular state management to divide the application's state into multiple small stores, rather than putting all the state in one large global store. This helps improve performance and maintainability.Avoid Multiple Mutation Submissions
In Vuex or Pinia, avoid multiple state updates during rendering. Try to merge multiple state modification operations into one batch submission to reduce the number of renderings.
Event and Method Optimization
Event Delegation
: Minimize binding events on each child element by binding events to the parent element through event delegation, reducing the number of event listeners.Use debounce or throttle to Optimize Frequently Triggered Events
Image and Resource Optimization
Lazy Loading Images
Use WebP Format
Caching and Server-Side Rendering (SSR)
Caching Strategy
For content that does not change frequently, use client-side caching or server-side caching techniques to reduce repeated requests and improve response speed.Server-Side Rendering (SSR) and Static Site Generation (SSG)