目錄
01 Functional components
02 Child component splitting
04 Reuse DOM with v-show
05 Keep-alive
09 Virtual scrolling
首頁 web前端 Vue.js Vue.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

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1657
14
CakePHP 教程
1415
52
Laravel 教程
1309
25
PHP教程
1257
29
C# 教程
1229
24
Go 框架的效能優化與橫向擴展技術? Go 框架的效能優化與橫向擴展技術? Jun 03, 2024 pm 07:27 PM

為了提高Go應用程式的效能,我們可以採取以下優化措施:快取:使用快取減少對底層儲存的存取次數,提高效能。並發:使用goroutine和channel並行執行冗長的任務。記憶體管理:手動管理記憶體(使用unsafe套件)以進一步優化效能。為了橫向擴展應用程序,我們可以實施以下技術:水平擴展(橫向擴展):在多個伺服器或節點上部署應用程式實例。負載平衡:使用負載平衡器將請求指派到多個應用程式執行個體。資料分片:將大型資料集分佈在多個資料庫或儲存節點上,提高查詢效能和可擴充性。

NGINX性能調整:針對速度和低潛伏期進行優化 NGINX性能調整:針對速度和低潛伏期進行優化 Apr 05, 2025 am 12:08 AM

Nginx性能調優可以通過調整worker進程數、連接池大小、啟用Gzip壓縮和HTTP/2協議、使用緩存和負載均衡來實現。 1.調整worker進程數和連接池大小:worker_processesauto;events{worker_connections1024;}。 2.啟用Gzip壓縮和HTTP/2協議:http{gzipon;server{listen443sslhttp2;}}。 3.使用緩存優化:http{proxy_cache_path/path/to/cachelevels=1:2k

優化之道:探尋java框架的效能提升之旅 優化之道:探尋java框架的效能提升之旅 Jun 01, 2024 pm 07:07 PM

透過實作快取機制、平行處理、資料庫最佳化和減少記憶體消耗,可以提升Java框架的效能。快取機制:減少資料庫或API請求次數,提高效能。並行處理:利用多核心CPU同時執行任務,提高吞吐量。資料庫最佳化:最佳化查詢、使用索引、設定連接池,提升資料庫效能。減少記憶體消耗:使用輕量級框架、避免洩漏、使用分析工具,減少記憶體消耗。

如何快速診斷 PHP 效能問題 如何快速診斷 PHP 效能問題 Jun 03, 2024 am 10:56 AM

快速診斷PHP效能問題的有效技術包括:使用Xdebug取得效能數據,然後分析Cachegrind輸出。使用Blackfire查看請求跟踪,產生效能報告。檢查資料庫查詢,識別低效率查詢。分析記憶體使用情況,查看記憶體分配和峰值使用。

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

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

異常處理對Java框架效能優化的影響 異常處理對Java框架效能優化的影響 Jun 03, 2024 pm 06:34 PM

異常處理會影響Java框架效能,因為異常發生時會暫停執行並處理異常邏輯。優化異常處理的技巧包括:使用特定異常類型快取異常訊息使用抑制異常避免過度的異常處理

Vue.js很難學習嗎? Vue.js很難學習嗎? Apr 04, 2025 am 12:02 AM

Vue.js不難學,特別是對於有JavaScript基礎的開發者。 1)其漸進式設計和響應式系統簡化了開發過程。 2)組件化開發讓代碼管理更高效。 3)使用示例展示了基本和高級用法。 4)常見錯誤可以通過VueDevtools調試。 5)性能優化和最佳實踐如使用v-if/v-show和key屬性可提升應用效率。

Java微服務架構中的效能最佳化 Java微服務架構中的效能最佳化 Jun 04, 2024 pm 12:43 PM

針對Java微服務架構的效能最佳化包含以下技巧:使用JVM調優工具來辨識並調整效能瓶頸。優化垃圾回收器,選擇並配置與應用程式需求相符的GC策略。使用快取服務(如Memcached或Redis)來提升回應時間並降低資料庫負載。採用非同步編程,以提高並發性和反應能力。拆分微服務,將大型單體應用程式分解成更小的服務,以提升可擴展性和效能。

See all articles