搜尋
首頁web前端Vue.jsVue.js中如何最佳化效能? 9個小技巧分享

Vue.js中如何最佳化效能? 9個小技巧分享

Apr 12, 2022 pm 08:45 PM
vue.js效能最佳化

Vue.js中如何最佳化效能?以下這篇文章跟大家分享Vue.js 效能優化的九個小技巧,希望對大家有幫助!

Vue.js中如何最佳化效能? 9個小技巧分享

(學習影片分享:vuejs教學

01 Functional components

Vue.js中如何最佳化效能? 9個小技巧分享

Vue.js中如何最佳化效能? 9個小技巧分享

#**原理:****函數式元件**與普通元件相比,它沒有狀態(沒有響應式資料),沒有實例(沒有this 上下文)。我們可以把函數式元件想像成元件裡的函數,入參是渲染上下文(render context),回傳值是渲染好的 HTML。正是因為函數式元件精簡了許多例如響應式和鉤子函數的處理,因此渲染性能會有一定提高。

適用場景:

  1. 不需要響應式資料及處理邏輯的純粹展示元件

  2. 用來標記或提供基本功能的高階元件

  3. 迴圈(v-for)中的元素

02 Child component splitting

Vue.js中如何最佳化效能? 9個小技巧分享

2-Vue.js中如何最佳化效能? 9個小技巧分享

#**原理:**在最佳化前的程式碼中,每次props 傳入的number 發生變化時都會重新渲染,在渲染的過程中會重新呼叫heavy 函數進行耗效能的運算。而優化後的程式碼邏輯是將複雜運算封裝在子元件內,由於Vue 的更新是元件粒度的,當傳入的number 發生變化時,父元件會重新渲染,而子元件由於並不依賴number 因此並不會重新渲染。執行計算的次數少了,效能自然也提升了。

**另:**這裡其實也可以用computed 計算屬性來優化(外部依賴沒有變化時不會重新計算,而且省去了額外渲染子元件的開銷)

03 Local variables

Vue.js中如何最佳化效能? 9個小技巧分享

3-Vue.js中如何最佳化效能? 9個小技巧分享

**原理:**對比前後程式碼可以發現差異在於:最佳化前的程式碼在進行計算時每次都直接引用this.base,而優化後的程式碼將this.base 使用局部變數base 進行了緩存,在之後的計算中都調用局部變數進行計算。為什麼會造成如此明顯的效能差異呢?原因在於每次存取 this.base 時,由於 this.base 是計算屬性,因此會執行一段邏輯程式碼查看現有的依賴項是否發生變化,如果發生變化則重新計算,沒有則傳回上一次計算值。這類計算邏輯的效能消耗在僅僅多呼叫幾次時可能還不明顯,但執行多了(類似範例每幀更新300 個元件,每個元件在一次更新內又呼叫了多次this.base)則會有比較大的性能差異。

04 Reuse DOM with v-show

Vue.js中如何最佳化效能? 9個小技巧分享

4-Vue.js中如何最佳化效能? 9個小技巧分享

#原則:

  • 實作方式:v-if 是動態的新增或刪除DOM 樹內或刪除DOM 元素,v-show 是透過設定DOM 元素的display 樣式屬性來控制顯隱。

  • 編譯過程:v-if 切換有一個局部編譯卸載的過程,切換過程中適當地銷毀和重建內部的事件監聽和子元件,v-show 只是簡單的基於CSS切換。

  • 編譯條件:v-if 是惰性的,如果初始條件為假,則什麼也不做,只有在條件第一次變成真時才開始局部編譯, v -show 是在任何條件下都被編譯,然後被緩存,而且DOM 元素保留。

  • 效能消耗:v-if 有更高的切換消耗,v-show 有更高的初始渲染消耗。

  • Usage scenarios: v-if is suitable for situations where conditions are unlikely to change, v-show is suitable for situations where conditions are frequently switched.

05 Keep-alive

Vue.js中如何最佳化效能? 9個小技巧分享

5-Vue.js中如何最佳化效能? 9個小技巧分享

5-Vue.js中如何最佳化效能? 9個小技巧分享

##**Principle: **In non-optimized scenarios, every time we click the button to switch the routing view, the component will be re-rendered. The rendering component will go through component initialization, render, patch and other processes. If the component is more complex , or if the nesting is deeper, the entire rendering will take a long time. After using KeepAlive, the vnode and DOM of the component wrapped by KeepAlive will be cached after the first rendering, and then the next time the component is rendered again, the corresponding vnode and DOM will be obtained directly from the cache. Then for rendering, there is no need to go through a series of processes such as component initialization, render and patch, etc., which reduces the script execution time and provides better performance.

But using the KeepAlive component is not without cost, because it will occupy more memory for caching. This is a typical application of space-for-time optimization ideas.

06 Deferred features

Vue.js中如何最佳化效能? 9個小技巧分享

where deferMixin is as follows:

6-Vue.js中如何最佳化效能? 9個小技巧分享

6-Vue.js中如何最佳化效能? 9個小技巧分享

6-Vue.js中如何最佳化效能? 9個小技巧分享

**Principle: **The main idea of ​​Defer is to split one rendering of a component into multiple times. It maintains the displayPriority variable internally, and then passes requestAnimationFrame It is incremented each frame when rendering, up to count. Then within the component using the Defer mixin, you can use v-if="defer(xxx)" to control the rendering of certain blocks when the displayPriority is increased to xxx.

When you have components that take time to render, it is a good idea to use Deferred for progressive rendering. It can avoid rendering being stuck due to JS execution taking too long.

07 Time slicing

Vue.js中如何最佳化效能? 9個小技巧分享

7-Vue.js中如何最佳化效能? 9個小技巧分享

7-Vue.js中如何最佳化效能? 9個小技巧分享##** Principle: **Using time slicing can avoid submitting too much data at one time, causing the internal JS execution time to be too long, blocking the UI process and causing the page to freeze.

**Another: **When executing time-consuming task processing, we usually add a loading effect, but through comparison before and after optimization, we can find that before optimization, JS has been running for a long time, blocking the UI process, so it does not The loading animation will not be displayed; after optimization, since the time-consuming task is divided into multiple time slices for submission, the single JS running time is shortened, and the loading animation also has a chance to be rendered.

08 Non-reactive data

Vue.js中如何最佳化效能? 9個小技巧分享

8-Vue.js中如何最佳化效能? 9個小技巧分享

## **Principle:** When submitting data internally, the newly submitted data will be defined as responsive by default. If the sub-property of the object is an object, the sub-property will also be recursively made responsive. Therefore, when too much data is submitted, the whole process is very time-consuming. After optimization, the attribute flag configurable in data is manually changed to false, so that the object attribute array obtained internally through Object.keys(obj) will ignore data, and the data attribute will not be definedReactive, because data points to an object. , which will also reduce the recursive response logic, which is equivalent to reducing the performance loss of this part. The larger the amount of data, the more obvious the effect of this optimization will be. 8-Vue.js中如何最佳化效能? 9個小技巧分享

The difference between setting configurable and using Object.freeze directly is:

**configurable: false**

is used to prevent changes and deletions of attribute flags, but allows changes The value of the object;

**Object.freeze(obj)** Adding/removing/changing attributes is prohibited. Set configurable: false, writable: false for all existing properties.

// configurable: false

let user = {
  name: "John"
};

Object.defineProperty(user, "name", {
  configurable: false
});

user.name = "Pete"; // 正常工作
delete user.name; // Error

// Object.freeze(obj)

let user = {
  name: "John"
};

Object.freeze(user);

user.name = "Pete";
console.log(user.name); // "John"复制代码

09 Virtual scrolling

Vue.js中如何最佳化效能? 9個小技巧分享

**Principle:**Virtual scrolling is implemented by rendering only the DOM within the view range , the performance will naturally be much better when rendering less content. The virtual scrolling component is also written by Guillaume Chau. Interested students can study its source code implementation. The basic principle is to monitor scrolling events, dynamically update the DOM elements that need to be displayed, and calculate Their displacement within the view. The virtual scrolling component is not without cost, because it needs to be calculated in real time during the scrolling process, so there will be a certain script execution cost. Therefore, if the amount of data in the list is not very large, it is enough for us to use ordinary scrolling

This article is reproduced from: https://juejin.cn/post/7084809333740929061

(Learning video sharing: web front-end development)

以上是Vue.js中如何最佳化效能? 9個小技巧分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:掘金社区。如有侵權,請聯絡admin@php.cn刪除
框架的選擇:是什麼推動了Netflix的決定?框架的選擇:是什麼推動了Netflix的決定?Apr 13, 2025 am 12:05 AM

Netflix在框架選擇上主要考慮性能、可擴展性、開發效率、生態系統、技術債務和維護成本。 1.性能與可擴展性:選擇Java和SpringBoot以高效處理海量數據和高並發請求。 2.開發效率與生態系統:使用React提升前端開發效率,利用其豐富的生態系統。 3.技術債務與維護成本:選擇Node.js構建微服務,降低維護成本和技術債務。

反應,vue和Netflix前端的未來反應,vue和Netflix前端的未來Apr 12, 2025 am 12:12 AM

Netflix主要使用React作為前端框架,輔以Vue用於特定功能。 1)React的組件化和虛擬DOM提升了Netflix應用的性能和開發效率。 2)Vue在Netflix的內部工具和小型項目中應用,其靈活性和易用性是關鍵。

前端中的vue.js:現實世界的應用程序和示例前端中的vue.js:現實世界的應用程序和示例Apr 11, 2025 am 12:12 AM

Vue.js是一種漸進式JavaScript框架,適用於構建複雜的用戶界面。 1)其核心概念包括響應式數據、組件化和虛擬DOM。 2)實際應用中,可以通過構建Todo應用和集成VueRouter來展示其功能。 3)調試時,建議使用VueDevtools和console.log。 4)性能優化可通過v-if/v-show、列表渲染優化和異步加載組件等實現。

vue.js和React:了解關鍵差異vue.js和React:了解關鍵差異Apr 10, 2025 am 09:26 AM

Vue.js適合小型到中型項目,而React更適用於大型、複雜應用。 1.Vue.js的響應式系統通過依賴追踪自動更新DOM,易於管理數據變化。 2.React採用單向數據流,數據從父組件流向子組件,提供明確的數據流向和易於調試的結構。

vue.js vs.反應:特定於項目的考慮因素vue.js vs.反應:特定於項目的考慮因素Apr 09, 2025 am 12:01 AM

Vue.js適合中小型項目和快速迭代,React適用於大型複雜應用。 1)Vue.js易於上手,適用於團隊經驗不足或項目規模較小的情況。 2)React的生態系統更豐富,適合有高性能需求和復雜功能需求的項目。

vue怎麼a標籤跳轉vue怎麼a標籤跳轉Apr 08, 2025 am 09:24 AM

實現 Vue 中 a 標籤跳轉的方法包括:HTML 模板中使用 a 標籤指定 href 屬性。使用 Vue 路由的 router-link 組件。使用 JavaScript 的 this.$router.push() 方法。可通過 query 參數傳遞參數,並在 router 選項中配置路由以進行動態跳轉。

vue怎麼實現組件跳轉vue怎麼實現組件跳轉Apr 08, 2025 am 09:21 AM

Vue 中實現組件跳轉有以下方法:使用 router-link 和 <router-view> 組件進行超鏈接跳轉,指定 :to 屬性為目標路徑。直接使用 <router-view> 組件顯示當前路由渲染的組件。使用 router.push() 和 router.replace() 方法進行程序化導航,前者保存歷史記錄,後者替換當前路由不留記錄。

vue的div怎麼跳轉vue的div怎麼跳轉Apr 08, 2025 am 09:18 AM

Vue 中 div 元素跳轉的方法有兩種:使用 Vue Router,添加 router-link 組件。添加 @click 事件監聽器,調用 this.$router.push() 方法跳轉。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具