Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Web前端性能优化-2014

 Web前端性能优化-2014

百度BIT课程

leeight

July 30, 2014
Tweet

Other Decks in Programming

Transcript

  1. What happened? • 从输⼊入 URL 到浏览器接收的过程中发⽣生了什么? • 浏览器如何向⺴⽹网卡发送数据? • 数据如何从本机⺴⽹网卡发送到服务器?

    • 服务器接收到数据后会进⾏行哪些处理? • 服务器返回数据后浏览器如何处理? • 浏览器如何将⻚页⾯面展现出来?
  2. What happened? • 从输⼊入 URL 到浏览器接收的过程中发⽣生了什么? • 浏览器如何向⺴⽹网卡发送数据? • 数据如何从本机⺴⽹网卡发送到服务器?

    • 服务器接收到数据后会进⾏行哪些处理? • 服务器返回数据后浏览器如何处理? • 浏览器如何将⻚页⾯面展现出来?
  3. DNS ! ; <<>> DiG 9.8.3-P1 <<>> +trace www.baidu.com @8.8.8.8

    ;; global options: +cmd . 18409 IN NS j.root-servers.net. . 18409 IN NS b.root-servers.net. ;; Received 228 bytes from 8.8.8.8#53(8.8.8.8) in 127 ms ! com. 172800 IN NS l.gtld-servers.net. com. 172800 IN NS m.gtld-servers.net. ;; Received 491 bytes from 202.12.27.33#53(202.12.27.33) in 318 ms ! baidu.com. 172800 IN NS dns.baidu.com. baidu.com. 172800 IN NS ns2.baidu.com. baidu.com. 172800 IN NS ns3.baidu.com. baidu.com. 172800 IN NS ns4.baidu.com. baidu.com. 172800 IN NS ns7.baidu.com. ;; Received 201 bytes from 192.31.80.30#53(192.31.80.30) in 409 ms ! www.baidu.com. 1200 IN CNAME www.a.shifen.com. a.shifen.com. 1200 IN NS ns5.a.shifen.com. a.shifen.com. 1200 IN NS ns4.a.shifen.com. a.shifen.com. 1200 IN NS ns3.a.shifen.com. a.shifen.com. 1200 IN NS ns2.a.shifen.com. a.shifen.com. 1200 IN NS ns1.a.shifen.com. ;; Received 228 bytes from 220.181.37.10#53(220.181.37.10) in 30 ms
  4. 扩展阅读 • TCP Fast Open • SYN 包传输数据,降低 ~15% •

    Building Blocks of TCP • QUIC(Quick UDP Internet Connections) • 0 - RTT • 避免 HOLB
  5. HTTP 1.0 & 1.1 • 1996: RFC1945 • 1999: RFC2616

    • 2014: RFC7230, RFC7231, RFC7232, RFC7233, RFC7234, RFC7235
  6. HTTP 1.1 vs 1.0 • 新增的⼀一些特性 • Keep-Alive Connection •

    Chunked Encoding Transfer • Byte Range Requests • Cache Mechanisms • Request Pipeline • ……
  7. Keep-Alive Connection $ telnet www.baidu.com 80 Trying 61.135.169.125... Connected to

    www.a.shifen.com. Escape character is ‘^]'. GET / HTTP/1.0 ! HTTP/1.1 200 OK Content-Type: text/html Connection: Close ……… Connection closed by foreign host. $ telnet www.baidu.com 80 Trying 61.135.169.125... Connected to www.a.shifen.com. Escape character is ‘^]'. GET / HTTP/1.1 Host: www.baidu.com ! HTTP/1.1 200 OK … Content-Type: text/html Transfer-Encoding: chunked Connection: Keep-Alive … ! 3dd6 ……… 0
  8. Cache Mechanisms • Expires 和 Cache-Control ⼆二选⼀一 • 建议使⽤用Cache-Control,避免Request Peak

    • 主⻚页⾯面不设置,或者设置不缓存 Expires: -1 • Last-Modified 和 ETag ⼆二选⼀一 • 建议使⽤用ETag,更准确⼀一些
  9. 数据的传输 • 延迟很重要 • 减少DNS查询 / DNS预查询 / DNS结果缓存 •

    减少HTTP请求的数 • 尽量使⽤用CDN • 合理的配置服务器缓存策略 • 减少传输的内容⼤大⼩小 / 压缩⽂文本 / 删除⽆无⽤用的⽂文本 • 新的协议 SPDY, QUIC, HTTP 2
  10. HTML & CSS • PreloadScanner • 书写⾼高效的 CSS selectors •

    避免使⽤用 CSS expressions • 把CSS放到⻚页⾯面顶部 • 明确图⽚片的尺⼨寸 • 明确内容的编码 • ……
  11. PreloadScanner <!—— GOOD ——> <script src=“large.js”></script> <script src=“ad.js” async></script> !

    <!—— BAD ——> <script src=“large.js”></script> <script> var s = document.createElement(‘script’); s.src = “ad.js”; document.head.appendChild(s); </script>
  12. 书写⾼高效的 CSS selectors • Avoid a universal key selector. •

    Make your rules as specific as possible. • Remove redundant qualifiers. • Avoid using descendant selectors, especially those that specify redundant ancestors. • Use class selectors instead of descendant selectors. https://developers.google.com/speed/docs/best-practices/rendering#UseEfficientCSSSelectors
  13. Defining JIT •Finding  a  way  to  generate  native   code

      •Then  execute  the  native  code
  14. Defining JIT ! •unsigned  char[]  code  =  {    

       0x48,  0x89,  0xf8        0x48,  0x83,  0xc0,  0x04        0xc3 };
  15. Defining JIT •mov  %rdi,  %rax   •add  $4,  %rax  

    •ret function add4(num) { return num + 4; }
  16. Tagged pointer • 1010 1111 0101 0011 1100 0000 0000

    0000 • 1010 1111 0101 0011 1100 0000 0000 0000 1010 1111 0101 0011 1100 0000 0000 0000
  17. with statement function containsWith() { return 3; with({}) {} }

    ! containsWith(); %OptimizeFunctionOnNextCall(containsWith); containsWith(); var status = %GetOptimizationStatus(containsWith); console.log(status === 2);
  18. debugger var DEBUG = false; function main() { if (DEBUG)

    { debugger; } require(“./biz1”); require(“./biz2”); require(“./biz3”); require(“./biz4”); } var DEBUG = false; function main() { require(“./biz1”); require(“./biz2”); require(“./biz3”); require(“./biz4”); }
  19. arguments function fn1(a, b) { b = b || 10;

    return a + b; } ! function fn2() { var args = [].slice.call( arguments); } function fn3(a, opt_b) { var b = opt_b || 10; return a + b; } ! function fn4() { var args = []; for(var i = 0; i < arguments.length; i ++ ){ args[i] = arguments[i]; } }
  20. for-in function nonLocalKey1() { var obj = {} for(var key

    in obj); return function() { return key; }; } ! var key; function nonLocalKey2() { var obj = {} for(key in obj); } function nonLocalKey3() { var obj = {} for(var key in obj); }
  21. References • https://developers.google.com/speed/docs/best-practices/rendering • https://developers.google.com/speed/articles/spdy-for-mobile • https://docs.google.com/spreadsheet/ccc? key=0As3TLupYw2RedG50WW9hNldQaERDTlFHMEc2S2FBTXc#gid=4 • https://www.igvita.com/

    • https://www.igvita.com/slides/2012/html5devconf/#52 • http://httparchive.org/trends.php#bytesTotal&reqTotal • http://http2.github.io/ • https://github.com/h5bp/server-configs • https://igrigorik.github.io/resource-hints/ • ……