前端性能揭秘

佘錦鑫(當軒)

  • 出版商: 電子工業
  • 出版日期: 2022-09-01
  • 售價: $600
  • 貴賓價: 9.5$570
  • 語言: 簡體中文
  • 頁數: 312
  • ISBN: 712144240X
  • ISBN-13: 9787121442407
  • 相關分類: 網頁設計
  • 立即出貨 (庫存 < 4)

買這商品的人也買了...

商品描述

本書主要介紹用於指導前端性能優化工作的通用優化方法,從網絡、瀏覽器、構建工具、跨端技術和CDN 等方面介紹不同技術、系統對性能的影響,同時幫助讀者瞭解如何有效優化性能。本書從性能的度量、分析和實驗這三個方面開始介紹。首先介紹性能優化的一些通用方法,然後將性能作為一個切麵幫助讀者瞭解與前端技術棧和性能有關的知識。從這個切麵觀察,這些系統的工作原理等知識被賦予了另外一層意義,通過這種聯系把工作原理真正運用到工作中,在性能優化方面發揮重要作用。本書面向的讀者為具有一定經驗的 Web 開發工程師,以及對前端開發或 Web 開發有一定瞭解的開發人員。同時,假定讀者能夠進行簡單的網頁開發,並且具備相關的基礎知識。

作者簡介

佘錦鑫
畢業於江南大學理學院,阿里巴巴前前端技術專家,曾負責Alibaba.com海外版性能優化,講師、開源愛好者。現就職於網易云音樂,對Web性能、跨端、可視化搭建等領域有較深入的理解。

目錄大綱

第1 篇從Vite 起步
第1 章從實踐開始··························.2
1.1 Hello World ···························.2
現在開始··························.2
使用DevTools ····················.4
第一個優化·······················.6
1.2 現實開發的例子······················.7
設置開發環境·····················.7
Vite ································.8
vite build ··························.9
進一步優化·····················.11
引入antd ························.11
按需引入························.13
動態import ·····················.14
1.3 小結································.15

第2 篇性能優化方法論
第2 章度量································.18
2.1 科學的方法·························.19
從一個客戶反饋說起··········.19
不度量性能,就無法優化
性能······························.19
真實的用戶端性能·············.20
2.2 初識Performance API ··············.21
performance.now()方法·········.21
構建首屏指標···················.23
2.3 均值、分位數和秒開率············.23
均值·····························.24
分位數···························.25
秒開率···························.26
如何選擇合適的統計指標·····.26
2.4 度量首屏···························.27
FP ································.27
FCP ······························.27
FMP ·····························.28
如何度量FMP ··················.28
選定並度量首屏················.30
2.5 度量流暢度·························.30
度量流暢度的指標·············.31
可視化工具·····················.31
用戶端度量·····················.32
2.6 Core Web Vitals ····················.34
LCP ······························.34
FID ·······························.38
CLS ······························.39
2.6 小結·································.41

第3 章分析································.42
3.1 分析方法···························.43
確定目標························.43
收集數據························.43
清洗數據························.44
統計值分析·····················.44
時序分析························.45
維度分析························.46
相關性分析·····················.48
3.2 常用的過程指標····················.48
TTFB ····························.49
DOMReady 和Load ············.50
3.3 Performance API 詳解··············.51
Navigation Timing API ·········.51
Peformance Entry API ··········.53
Resource Timing ················.54
Navigation Timing Level 2 ·····.55
Paint Timing ····················.56
User Timing ·····················.56
3.4 分階段性能分析···················.58
常用的指標·····················.58
其他值得分析的指標··········.59
3.5 小結································.59

第4 章實驗································.60
4.1 優化不是照搬軍規·················.61
時代在發展·····················.61
優化的木桶效應明顯··········.62
用戶環境差異大················.62
性能實驗························.62
4.2 用實驗驗證優化···················.63
混沌問題························.64
設計實驗························.64
分桶·····························.65
上報和分析數據················.68
A/B Test 背後的數學···········.68
結論不重要,重要的是方法··.69
4.3 用實驗改進優化···················.69
建立模型························.69
實驗修正························.70
4.4 小結································.71

第5 章工具································.72
5.1 DevTools ···························.73
Network 面板···················.73
Performance 面板···············.76
5.2 WebPageTest ·······················.81
發起測試························.82
報告······························.83
Waterfall 視圖··················.83
5.3 小結································.87

第3 篇網絡協議與性能
第6 章TTFB 為什麼這麼長··············.90
6.1 TTFB 的合理值····················.91
精確定義························.92
RTT ······························.92
RTT 一般需要多久·············.93
TTFB 的構成····················.93
實驗環境驗證···················.94
6.2 如何優化TTFB ····················.95
減少請求的傳輸量·············.96
減少服務器端的處理時間·····.96
減少RTT ························.98
TTFB 的值越小越好嗎·········.98
6.3 小結·································.99

第7 章建立連接為什麼這麼慢·········.100
7.1 建立連接應該耗時多久···········.101
TCP 協議·······················.101
建立連接需要多少個RTT ····.101
抓包驗證·······················.102
7.2 如何優化建立連接的耗時········.103
減少物理距離··················.103
preconnect ······················.103
復用連接·······················.103
域名收攏·······················.104
TCP Fast Open ·················.104
QUIC 和HTTP/3 ··············.104
7.3 小結·······························.105

第8 章Fetch 之前瀏覽器在幹什麼····.106
8.1 重定向·····························.107
HTML 重定向·················.109
有哪些重定向·················.109
8.2 瀏覽器打開耗時··················.112
初始化標籤頁的時間·········.112
unload 的耗時··················.112
8.3 如何優化beforeFetch 耗時·······.114
重定向邏輯前置···············.115
合併重定向····················.115
避免使用短鏈·················.116
使用beforeFetch 度量和
分析····························.116
8.4 小結·······························.117

第9 章HTTPS 協議比HTTP 協議
更慢嗎····························.118
9.1 HTTPS 協議為什麼安全··········.119
對稱加密和非對稱加密·······.119
SSL/TLS 的實現···············.120
SSL/TLS 握手·················.122
TLS False Start ·················.124
TLS 1.3 ·························.124
9.2 HTTPS 協議如何吊銷證書········.125
CRL ·····························.125
OCSP ···························.126
OCSP Stapling ·················.126
瀏覽器支持的情況············.126
證書類型·······················.127
證書驗證機制對性能的影響···.129
9.3 HTTPS 協議更慢嗎···············.129
確保證書鏈完整···············.129
啟用TLS 1.3 ···················.129
不濫用EV 證書···············.130
開啟OSCP Stapling ···········.130
9.4 小結································.130

第10 章HTTP/2、HTTP/3 和性能·····.131
10.1 HTTP/2 和性能···················.131
連接復用為什麼不生效······.131
頭部壓縮對我們有什麼影響··.137
為什麼沒有廣泛使用Server
Push ···························.140
10.2 為什麼還需要HTTP/3 ···········.144
HTTP/2 存在什麼問題·······.145
HTTP/3 如何解決問題·······.146
10.3 小結······························.148

第11 章壓縮和緩存·····················.150
11.1 傳輸速度和壓縮速度如何兼得···.151
Content-Encoding ·············.151
gzip 壓縮和br 壓縮··········.152
實時壓縮······················.152
離線壓縮······················.153
如何優化傳輸性能···········.154
11.2 HTTP 緩存什麼時候會失效·····.154
緩存不僅僅是瀏覽器的
事情···························.154
緩存Header ···················.154
11.3 小結······························.157

第4 篇瀏覽器與性能
第12 章瀏覽器和性能···················.160
12.1 第一次渲染時都發生了什麼····.161
最小的渲染路徑··············.162
盡快返回HTML ··············.167
減少資源的阻塞··············.167
12.2 為什麼DOM 操作很慢··········.168
幀··································.168
重排·······························.169
重繪·······························.170
訪問DOM 屬性················.170
如何優化DOM 操作··········.171
12.3 小結······························.172

第13 章異步任務和性能················.173
13.1 事件循環機制····················.174
為什麼要有事件循環········.174
多線程阻塞模型··············.174
事件循環······················.175
13.2 宏任務和微任務·················.176
13.3 Promise 的polyfill 性能··········.178
如何正確實現Promise ·······.178
13.4 requestAnimationFrame ··········.180
13.5 小結······························.181

第14 章內存為什麼會影響性能········.182
14.1 內存······························.182
內存管理·························.183
14.2 內存洩漏·························.188
內存洩漏和性能··············.188
常見的導致內存洩漏的原因··.188
內存洩漏問題的診斷工具···.189
14.3 小結······························.191

第15 章使用ServiceWorker 改善
性能······························.193
15.1 ServiceWorker 概述··············.194
AppCache······················.194
ServiceWorker·················.195
ServiceWorker 能做什麼·····.195
15.2 使用ServiceWorker 進行緩存···.196
Cache API ·····················.196
IndexDB ·······················.201
控制緩存的Cache Key ······.201
更加靈活的緩存更新策略···.203
15.3 API 提前加載····················.204
15.4 ServiceWorker 冷啟動···········.205
開啟Navigation Preload ·····.206
消費Navigation Preload ·····.206
15.5 小結······························.207

第16 章字體對性能的影響·············.208
16.1 字體導致的佈局偏移············.208
如何定位佈局偏移···········.208
16.2 如何避免字體帶來的佈局偏移···.210
如何盡快加載字體···········.211
字體文件的格式··············.211
字體的加載···················.212
預加載字體···················.213
裁剪字體的大小··············.214
16.3 小結······························.214

第5 篇前端工程與性能
第17 章構建工具和性能················.218
17.1 為什麼需要打包·················.219
CommonJS ····················.220
AMD ···························.220
CMD ···························.221
異步模塊加載器··············.222
依賴加載優化················.223
模塊打包器···················.224
ES Module ····················.225
17.2 構建工具可以做什麼············.226
構建工具和構建優化········.227
為什麼要優化打包體積·····.227
Bundle 分析···················.228
Tree Shaking ··················.229
Scope Hoisting ················.231
Code Splitting ·················.233
代碼壓縮······················.234
Vite 和Bundleless ············.237
17.3 小結······························.237

第18 章服務器端渲染和性能··········.239
18.1 SSR 和同構······················.241
18.2 SSR 的性能優化·················.241
緩存·······························.242
數據預取·························.245
按需渲染·························.245
流式渲染·························.246
18.3 小結······························.246

第6 篇跨端技術與CDN
第19 章WebView 和性能···············.248
19.1 WebView 和Native 的區別······.249
LayoutInflater ·················.249
加載XML 的具體過程······.250
Measure ························.250
Layout ·························.251
Paint ···························.252
Surface ·························.253
SurfaceFlinger ·················.253
差異····························.253
19.2 WebView 的通信成本···········.254
JavaScript 調用Native ········.254
Native 調用JavaScript ········.258
雙向通信······················.258
通信對性能的影響···········.259
減少通信數據量··············.259
避免頻繁通信·················.259
19.3 React Native 的懶加載有何
不同······························.260
Web 實現·····················.260
基於滾動容器的懶加載·····.260
基於位置獲取的懶加載·····.262
虛擬列表······················.263
19.4 React Native 如何減小打包
體積······························.265
Metro ··························.265
度量···························.266
分析···························.266
手動Tree Shaking ············.267
利用Babel 插件進行優化···.269
體積和性能的關係···········.271
19.5 API 並行請求····················.271
發起請求·························.272
請求攔截·························.273
一致性檢驗······················.274
命中率分析······················.274
19.6 小結······························.274

第20 章CDN 和性能····················.275
20.1 什麼是CDN ······················.275
解析·······························.276
邊緣節點·························.276
回源·······························.277
緩存策略·························.277
20.2 如何提升緩存命中率············.278
如何在端側統計緩存命中的
情況····························.278
減少緩存分裂·················.279
緩存忽略動態參數···········.279
歸一化Vary Header ··········.280
長效緩存······················.280
20.3 動態加速·························.281
海外加速·························.282
連接復用·························.282
客戶端連接復用················.282
HTTPS 優化·····················.283
動靜分離·························.283
壓縮·······························.284
什麼場景適合使用動態加速··.284
20.4 自動polyfill ······················.284
什麼是polyfill ················.284
Polyfill.io ······················.285
實現原理······················.287
20.5 邊緣計算和性能·················.288
CDN 的可編程功能··········.288
Hello World ···················.289
自定義Cache Key ············.289
前置重定向···················.290
流式渲染······················.290
20.6 小結······························.291