Vue.js中如何最佳化效能? 9個小技巧分享
Vue.js中如何最佳化效能?以下這篇文章跟大家分享Vue.js 效能優化的九個小技巧,希望對大家有幫助!
(學習影片分享:vuejs教學)
01 Functional components
#**原理:****函數式元件**與普通元件相比,它沒有狀態(沒有響應式資料),沒有實例(沒有this 上下文)。我們可以把函數式元件想像成元件裡的函數,入參是渲染上下文(render context),回傳值是渲染好的 HTML。正是因為函數式元件精簡了許多例如響應式和鉤子函數的處理,因此渲染性能會有一定提高。
適用場景:
不需要響應式資料及處理邏輯的純粹展示元件
用來標記或提供基本功能的高階元件
迴圈(v-for)中的元素
02 Child component splitting
#**原理:**在最佳化前的程式碼中,每次props 傳入的number 發生變化時都會重新渲染,在渲染的過程中會重新呼叫heavy 函數進行耗效能的運算。而優化後的程式碼邏輯是將複雜運算封裝在子元件內,由於Vue 的更新是元件粒度的,當傳入的number 發生變化時,父元件會重新渲染,而子元件由於並不依賴number 因此並不會重新渲染。執行計算的次數少了,效能自然也提升了。
**另:**這裡其實也可以用computed 計算屬性來優化(外部依賴沒有變化時不會重新計算,而且省去了額外渲染子元件的開銷)
03 Local variables
**原理:**對比前後程式碼可以發現差異在於:最佳化前的程式碼在進行計算時每次都直接引用this.base,而優化後的程式碼將this.base 使用局部變數base 進行了緩存,在之後的計算中都調用局部變數進行計算。為什麼會造成如此明顯的效能差異呢?原因在於每次存取 this.base 時,由於 this.base 是計算屬性,因此會執行一段邏輯程式碼查看現有的依賴項是否發生變化,如果發生變化則重新計算,沒有則傳回上一次計算值。這類計算邏輯的效能消耗在僅僅多呼叫幾次時可能還不明顯,但執行多了(類似範例每幀更新300 個元件,每個元件在一次更新內又呼叫了多次this.base)則會有比較大的性能差異。
04 Reuse DOM with v-show
#原則:
實作方式: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
06 Deferred features
07 Time slicing
##** 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## **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.
**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
**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中文網其他相關文章!

熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

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

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

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

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

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

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

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

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