f9yx0du 发表于 2024-10-10 05:02:54

Web前端性能优化思路


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">本文旨在整理<span style="color: black;">平常</span>Web前端性能优化的思路,可供前端<span style="color: black;">研发</span>参考。<span style="color: black;">由于</span>力求精简,限于篇幅,<span style="color: black;">因此</span>并未详述<span style="color: black;">详细</span>实施<span style="color: black;">方法</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">基于现代Web前端框架的应用,其原理是<span style="color: black;">经过</span>浏览器向服务器发送网络请求,获取必要的index.html和打包好的JS、CSS等资源,在浏览器内执行JS,动态获取数据并渲染页面,从而将结果呈现给用户。</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/93ee64cbad124181bed378107a5cdb42~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728744227&amp;x-signature=smA9tXH0OvIlyAWVicZpaHnBkfk%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在这个过程中,有两个<span style="color: black;">过程</span>可能较为耗时,一个是网络资源的加载,另一个是浏览器内代码执行和DOM渲染。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而耗时的<span style="color: black;">增多</span>会<span style="color: black;">引起</span>页面响应慢,卡顿,影响用户体验。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">针对<span style="color: black;">以上</span>两种耗时的<span style="color: black;">状况</span>,<span style="color: black;">平常</span>的优化方向有:</span></p><span style="color: black;">缩短请求耗时;</span><span style="color: black;">减少重排重绘;</span><span style="color: black;">改善JS性能。</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1 缩短请求耗时</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">网络资源是Web应用运行的<span style="color: black;">基本</span>,改善网络资源加载速度会<span style="color: black;">明显</span>改善前端性能。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1.1 优化打包资源</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">总体原则:</span></strong><span style="color: black;"> 减少或延迟模块引用,以减少网络负荷。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">常用工具:</span></strong></p><span style="color: black;">webpack</span><span style="color: black;">webpack-bundle-analyzer可视化分析工具</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">常用<span style="color: black;">办法</span>:</span></strong></p><span style="color: black;">减小体积:减少非必要的</span><span style="color: black;"><span style="color: black;">import</span></span><span style="color: black;">;压缩JS代码;配置服务器gzip等;<span style="color: black;">运用</span>WebP<span style="color: black;">照片</span>;</span><span style="color: black;">按需加载:可<span style="color: black;">按照</span>“路由”、“<span style="color: black;">是不是</span>可见”按需加载JS代码,减少初次加载JS体积。<span style="color: black;">例如</span><span style="color: black;">能够</span><span style="color: black;">运用</span></span><span style="color: black;"><span style="color: black;">import()</span></span><span style="color: black;">进行代码分割,按需加载;</span><span style="color: black;"><span style="color: black;">掰开</span>打包:利用浏览器缓存机制,依据模块更新频率分层打包。</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">其他<span style="color: black;">办法</span>:</span></strong></p><span style="color: black;">雪碧图:<span style="color: black;">每一个</span>HTTP/1.1请求都是独立的TCP连接,最大6个并发,<span style="color: black;">因此</span>合并<span style="color: black;">照片</span>资源<span style="color: black;">能够</span>优化加载速度。HTTP/2<span style="color: black;">已然</span>不需要这么做了。</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1.2 CDN加速</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">总体原则:</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> <span style="color: black;">经过</span>分布式的边缘网络节点,缩短资源到终端用户的<span style="color: black;">拜访</span>延迟。</p>
    <strong style="color: blue;"><span style="color: black;">常用工具:</span></strong><span style="color: black;">Cloudflare</span><span style="color: black;">AWS CloudFront</span><span style="color: black;">Aliyun CDN</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">常用<span style="color: black;">办法</span>:</span></strong></p><span style="color: black;">加速<span style="color: black;">照片</span>、视频等大体积文件</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1.3 浏览器缓存</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">总体原则:</span></strong><span style="color: black;">避免重复传输相同的数据,节省网络带宽,加速资源获取。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">常用<span style="color: black;">办法</span>:</span></strong><span style="color: black;"><span style="color: black;">能够</span>通过设置HTTP Header来<span style="color: black;">掌控</span>缓存策略,<span style="color: black;">通常</span>有如下几种。</span></p><span style="color: black;">强缓存</span><span style="color: black;"><span style="color: black;">Expires</span></span><span style="color: black;">:HTTP/1.0</span><span style="color: black;"><span style="color: black;">Cache-Control</span></span><span style="color: black;">:HTTP/1.1</span><span style="color: black;">协商缓存</span><span style="color: black;"><span style="color: black;">ETag</span></span><span style="color: black;"> + </span><span style="color: black;"><span style="color: black;">If-None-Match</span></span><span style="color: black;"><span style="color: black;">Last-Modified</span></span><span style="color: black;"> + </span><span style="color: black;"><span style="color: black;">If-Modified-Since</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">拿ETag举例,<span style="color: black;">倘若</span>浏览器给的</span><span style="color: black;"><span style="color: black;">If-None-Match</span></span><span style="color: black;">值与服务端给的</span><span style="color: black;"><span style="color: black;">ETag</span></span><span style="color: black;">值相等,服务器就直接返回</span><span style="color: black;"><span style="color: black;">304</span></span><span style="color: black;">,从而避免重复传输数据。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">ETag示例:</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/701d94fe646d43c08115a09ee5adb400~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728744227&amp;x-signature=g8WSEg660DJJCERdlLFcUGEjrt0%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>几个配置<span style="color: black;">同期</span>存在,则优先级为:</span><span style="color: black;"><span style="color: black;">Cache-Control</span></span><span style="color: black;"> &gt; </span><span style="color: black;"><span style="color: black;">Expires</span></span><span style="color: black;"> &gt; </span><span style="color: black;"><span style="color: black;">ETag</span></span><span style="color: black;"> &gt; </span><span style="color: black;"><span style="color: black;">Last-Modified</span></span><span style="color: black;">。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1.4 更高版本的HTTP</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">总体原则:</span></strong><span style="color: black;"><span style="color: black;">运用</span>高版本HTTP<span style="color: black;">提高</span>性能。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">常用工具:</span></strong></p><span style="color: black;">HTTP/2</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">HTTP/2较HTTP/1.1最大的改进在于:</span></p><span style="color: black;">多路复用:单一TCP连接,多HTTP请求,有Demo;</span><span style="color: black;">头部压缩:减少HTTP头体积;</span><span style="color: black;">请求优先级:优先获取重要的数据;</span><span style="color: black;">服务端推送:主动推送CSS等静态资源。</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">其他<span style="color: black;">办法</span>:</span></strong></p><span style="color: black;">HTTP/3</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">HTTP/3基于UDP,有<span style="color: black;">非常多</span>方面的性能改进,如多路复用无队头阻塞,响应更快。感兴趣的<span style="color: black;">朋友</span>可参考Wiki。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1.5 Web Socket</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">总体原则:</span></strong><span style="color: black;"><span style="color: black;">处理</span>HTTP协议<span style="color: black;">没法</span>实时通信的问题。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Web Socket是一条有状态的TCP长连接,用于实现实时通信、实时响应。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">1.6 服务器端渲染(SSR)</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">总体原则:</span></strong><span style="color: black;"><span style="color: black;">第1</span>次<span style="color: black;">拜访</span>时,服务器端直接返回渲染好的页面。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;"><span style="color: black;">通常</span>流程:</span></strong></p><span style="color: black;">浏览器向 URL 发送请求;</span><span style="color: black;">服务器端返回“空白”</span><span style="color: black;"><span style="color: black;">index.html</span></span><span style="color: black;">;</span><span style="color: black;">浏览器<span style="color: black;">不可</span>呈现页面,需要继续下载依赖;</span><span style="color: black;">加载所有脚本后,组件<span style="color: black;">才可</span>被渲染。</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">SSR流程:</span></strong></p><span style="color: black;">浏览器向 URL 发送请求;</span><span style="color: black;">服务器端执行JS完成首屏渲染并返回;</span><span style="color: black;">浏览器直接呈现页面,<span style="color: black;">而后</span>继续下载其他依赖;</span><span style="color: black;">加载所有脚本后,组件将再次在客户端呈现。它将对现有View进行合并。</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">常用工具:</span></strong></p><span style="color: black;">Node.js,用于服务器端执行代码,输出HTML给浏览器,支持所有主流前端框架</span><span style="color: black;">Next.js,用于服务器端渲染React的框架</span><span style="color: black;">gatsby,用React生成静态网站的工具</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">除了<span style="color: black;">能够</span><span style="color: black;">提高</span>页面用户体验,还能应用于SEO。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">2 减少重排重绘</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">除了网络资源以外,另一个影响前端性能的<span style="color: black;">原因</span><span style="color: black;">便是</span>前端页面的渲染绘制效率。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">虽然<span style="color: black;">区别</span>的前端框架有<span style="color: black;">有些</span>差异,但整体的优化思路是一致的,<span style="color: black;">这儿</span>将以React举例。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">2.1 减少渲染量</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">总体原则:</span></strong><span style="color: black;">不渲染未展示的部分。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">常用工具:</span></strong></p><span style="color: black;">react-window</span><span style="color: black;">react-loadable</span><span style="color: black;">JS原生,如</span><span style="color: black;"><span style="color: black;">IntersectionObserver</span></span><span style="color: black;">框架<span style="color: black;">供给</span>,如</span><span style="color: black;"><span style="color: black;">React.lazy</span></span><span style="color: black;">、</span><span style="color: black;"><span style="color: black;">react-intersection-observer</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">常用<span style="color: black;">办法</span>:</span></strong></p><span style="color: black;">虚拟列表:只渲染可见区;</span><span style="color: black;">惰性加载:无限滚动;</span><span style="color: black;">按需加载:页面<span style="color: black;">仅在</span>切换过去时才加载。</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">以虚拟列表举例,以下是<span style="color: black;">运用</span></span><span style="color: black;"><span style="color: black;">react-window</span></span><span style="color: black;">库,仅仅渲染了可见区的数据:</span></p>
    <div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/6c67acfc9dc243168146fdfcce46789f~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728744227&amp;x-signature=tzh%2FPy1gD6I6OpxaRmP5cQlcaoM%3D" style="width: 50%; margin-bottom: 20px;"></div>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">2.2 减少渲染次数</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">总体思路:</span></strong><span style="color: black;">避免重复的渲染。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">常用工具:</span></strong></p><span style="color: black;">lodash</span><span style="color: black;">JS或框架自带</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">常用<span style="color: black;">办法</span>:</span></strong></p><span style="color: black;">防抖与节流;</span><span style="color: black;"><span style="color: black;">针对</span>React函数组件<span style="color: black;">来讲</span>,<span style="color: black;">恰当</span><span style="color: black;">运用</span>副<span style="color: black;">功效</span>,拆分无<span style="color: black;">相关</span>的副<span style="color: black;">功效</span>;</span><span style="color: black;"><span style="color: black;">针对</span>React类组件<span style="color: black;">来讲</span>,<span style="color: black;">能够</span><span style="color: black;">运用</span></span><span style="color: black;"><span style="color: black;">shouldComponentUpdate</span></span><span style="color: black;">或<span style="color: black;">运用</span></span><span style="color: black;"><span style="color: black;">PureComponent</span></span><span style="color: black;">来优化渲染;</span><span style="color: black;">利用缓存,如React.memo;</span><span style="color: black;"><span style="color: black;">运用</span>requestAnimationFrame替代</span><span style="color: black;"><span style="color: black;">setInterval</span></span><span style="color: black;">执行动画。</span>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">3 改善JS性能</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">由于</span>浏览器是单线程异步模型,<span style="color: black;">长期</span>的运算会阻塞渲染过程,<span style="color: black;">因此</span>改善<span style="color: black;">繁杂</span>运算有助于改善前端的整体性能。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">3.1 缓存<span style="color: black;">繁杂</span>计算</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">总体思路:</span></strong><span style="color: black;">避免重复计算。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">常用<span style="color: black;">办法</span>:</span></strong></p><span style="color: black;"><span style="color: black;">针对</span>React函数组件<span style="color: black;">来讲</span>,<span style="color: black;">能够</span><span style="color: black;">运用</span></span><span style="color: black;"><span style="color: black;">useMemo</span></span><span style="color: black;">缓存<span style="color: black;">繁杂</span>计算值。</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">举例如下,</span><span style="color: black;"><span style="color: black;">memoizedValue</span></span><span style="color: black;">需要经过<span style="color: black;">繁杂</span>计算<span style="color: black;">才可</span>得到,此时就<span style="color: black;">能够</span><span style="color: black;">运用</span></span><span style="color: black;"><span style="color: black;">useMemo</span></span><span style="color: black;">缓存,仅仅在输入参数<span style="color: black;">出现</span>变化时才重新计算,避免计算阻塞页面渲染,从而避免页面卡顿。</span></p><span style="color: black;">1</span><span style="color: black;">const</span> MyFunctionalComponent = <span style="color: black;"><span style="color: black;">()</span> =&gt;</span> {
    <span style="color: black;">2</span> <span style="color: black;">const</span> memoizedValue = useMemo(<span style="color: black;"><span style="color: black;">()</span> =&gt;</span> {
    <span style="color: black;">3</span>computeExpensiveValue(a, b);<span style="color: black;">4</span> }, );
    <span style="color: black;">5</span>
    <span style="color: black;">6</span> <span style="color: black;">return</span> <span style="color: black;"><span style="color: black;">&lt;<span style="color: black;">AComponent</span> <span style="color: black;">value</span>=<span style="color: black;">{memoizedValue}/</span>&gt;</span>;
      7}</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">但</span><span style="color: black;"><span style="color: black;">useMemo</span></span><span style="color: black;"><span style="color: black;">自己</span><span style="color: black;">亦</span>有性能消耗,需要视<span style="color: black;">状况</span><span style="color: black;">运用</span>,某些场景<span style="color: black;">能够</span>利用React的渲染机制避免性能问题,<span style="color: black;">能够</span>参考《Before You memo()》。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">3.2 Web Worker</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">总体原则:</span></strong><span style="color: black;">多线程思想。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">常用<span style="color: black;">办法</span>:</span></strong></p><span style="color: black;">Dedicated Workers,处理与UI无关的密集型数学计算:大数据集合排序、数据压缩、音视频处理;</span><span style="color: black;">Service Worker,服务端推送,<span style="color: black;">或</span>PWA中<span style="color: black;">协同</span>CacheStorage在前端<span style="color: black;">掌控</span>缓存资源;</span><span style="color: black;">Shared Worker,Tab间通信。</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">JS语言在设计之初<span style="color: black;">便是</span>单线程异步模型,好处是<span style="color: black;">能够</span><span style="color: black;">有效</span>处理I/O操作,但坏处是<span style="color: black;">没法</span>利用多核CPU。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">Web Worker会<span style="color: black;">起步</span>系统级别的线程,可进行多线程编程,发挥多核的性能。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">3.3 Web Assembly</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">总体原则:</span></strong><span style="color: black;">将<span style="color: black;">繁杂</span>的计算<span style="color: black;">规律</span>编译为Web Assembly,避免JS类型推断过程中的性能开销,可用于性能的极限优化。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">适用范围有限:</span></strong></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">曾在网上看到,有人<span style="color: black;">运用</span>自顶向下非优化的斐波那契数列算法来举例,说Web Assembly比原生JS快一倍,实测之后似乎<span style="color: black;">亦</span><span style="color: black;">无</span>。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">在同一台<span style="color: black;">设备</span>测试,其中求第48个值的耗时如下:</span></p><span style="color: black;">C(Ubuntu+GCC):18s</span><span style="color: black;">JS(V8):32s</span><span style="color: black;">Web Assembly(V8+EMCC):39s</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">一种可能的猜想是,斐波那契计算中<span style="color: black;">无</span><span style="color: black;">海量</span>的类型推断,<span style="color: black;">况且</span>V8内部有<span style="color: black;">有些</span>优化机制,使得此处JS执行速度快于Web Assembly。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">简而言之,并非所有场景都适用于Web Assembly。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">另一种运用场景是,把<span style="color: black;">区别</span>语言编写的代码(C/C++/Java等)编译为Web Assembly,能以接近原生的速度在Web中运行,并且与JS共存。</span></p>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">总结</h1>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">引起</span>前端性能问题的<span style="color: black;">原因</span>是多方面的。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">倘若</span>是前端资源加载慢,<span style="color: black;">引起</span>页面慢,则应该<span style="color: black;">思虑</span><span style="color: black;">怎样</span>缩短请求耗时。而<span style="color: black;">倘若</span>是前端页面<span style="color: black;">规律</span>笨重,UI数据量太大,则<span style="color: black;">能够</span>试着从减少重排重绘的<span style="color: black;">方向</span>去优化。<span style="color: black;">针对</span>耗时长的<span style="color: black;">繁杂</span>计算,缓存计算结果<span style="color: black;">常常</span>是见效较快的优化方式。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">最后需要<span style="color: black;">重视</span>的是,在<span style="color: black;">实质</span>应用<span style="color: black;">研发</span>过程中,<span style="color: black;">由于</span>受限于<span style="color: black;">研发</span>成本,<span style="color: black;">因此</span>需要平衡优化所花的代价与其对应产生的成效。<span style="color: black;">能够</span>有针对性地对性能瓶颈进行分析和处理,<span style="color: black;">同期</span><span style="color: black;">亦</span>需要避免引入不必要的优化<span style="color: black;">办法</span>,以<span style="color: black;">保证</span><span style="color: black;">最后</span>优化效果。</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">文/Thoughtworks严文</span></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">原文链接:</p>https://insights.thoughtworks.cn/web-frontend-performance-tuning/
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">更加多</span>精彩洞见,请关注<span style="color: black;">微X</span>公众号Thoughtworks洞见。</span></p>




qzmjef 发表于 2024-10-18 03:35:42

真情实感,其含义为认真了、走心了的意思,是如今的饭圈常用语。

7wu1wm0 发表于 2024-11-6 08:37:27

外链论坛的成功举办,是与各位领导、同仁们的关怀和支持分不开的。在此,我谨代表公司向关心和支持论坛的各界人士表示最衷心的感谢!

7wu1wm0 发表于 6 天前

你的话语如春风拂面,让我心生暖意。
页: [1]
查看完整版本: Web前端性能优化思路