Taobao homepage performance optimization practice

In the above "Let's take a look at the personalization of Taobao's homepage", I took you to see the new homepage with a personalized flavor. The front end faces:

  • Many data sources

  • Serial request to render a module

  • Operational data and personalization data matching and management

  • data disaster recovery

and more questions. The Taobao homepage revision, although it no longer supports low-version antique browsers such as IE6 and IE7, there are still many factors that affect the performance of the homepage:

  • Too many dependent systems, data requests are divided into three parts, one is static resources (such as js/css/image/iconfont, etc.); the other is static data pushed to CDN (such as data filled in by operations, front-end configuration information, etc. ); the third is the back-end interface, different modules correspond to different businesses, and there are still a lot of advertisement content on the page. It is roughly estimated that there are 8 interface requests sent from the first screen when the page is just loaded. Scroll to the bottom, Had to make more than 20 requests.

  • The first screen data cannot be directly output. Many data on the first screen are obtained through asynchronous requests. Due to system limitations, these requests are unavoidable, and the number of requests is large, which greatly affects the first screen time.

  • There are too many modules. In order to be able to fill in data permissions between background isolation operations, the modules must be divided into fine-grained divisions, as shown in the following figure:

A simple module must be split into multiple industry small modules, and the same is true for other parts of the page, and these split modules may not be displayed, and the algorithm needs to tell the front end which modules to display.

  • There are too many pictures. Turning the page and scrolling down, it is obvious that there are pictures on the whole screen of the page. Some pictures are filled in by operation, and some pictures are provided by the personalized interface. These pictures have no fixed size.

Web page performance metrics

There are many web page performance metrics. If you can grasp the key ones and optimize them centrally, the performance will naturally improve.

FPS

One of the indicators that can best reflect the page performance is FPS (frame per second). Generally, the system sets the screen refresh rate to 60fps. When the page element is animated, scrolled or gradient, the drawing rate is less than 60, and it will not be smooth, and if it is less than 24, it will be unsmooth. Caton, if it is less than 12, it is basically considered that the card has exploded.

The duration of 1 frame is about 16ms. Excluding the system context switching overhead, each frame only leaves us about 10ms of program processing time. If the processing time of a script exceeds 10ms, then this frame can be considered lost. If processing If the time exceeds 26ms, it can be determined that two consecutive frames are lost, and so on. We cannot tolerate the continuous loss of five or six frames in the page, which means that we must find a way to split the code program whose execution time exceeds 80ms, which is not an easy job.

When the page is first loaded, many programs need to be initialized, and there may be a lot of time-consuming DOM operations, so the necessary operations in the first 1s will result in a very low frame rate, which we can ignore. Of course, this is for PC. Mobile has less content, and the amount of DOM and JS scripts is much smaller than that of PC. 1s may be a bit long.

DOMContentLoaded 和 Load

The DOMContentLoaded event will only be triggered after the DOM is loaded and the parsing is completed. If there is too much content in the source code output, the time for the client to parse the DOM will also increase. Don't underestimate the parsing time here. If the number of DOMs increases by 2000 and the nesting level is deeper , the parsing time will also increase by 50-200ms. This consumption is actually unnecessary for most pages. It is enough to ensure the output of the first screen. The subsequent content only retains hooks and uses JS to dynamically render.

Load time can be used to measure the total amount of information received by the client during the loading of the first screen. If the first screen is full of large-sized images or the client establishes a large number of connections with the backend, the Load time will be prolonged accordingly.

fluency

Fluency is the visual feedback to FPS. The higher the FPS value, the smoother the visual presentation. In order to ensure the loading speed of the page, many content will not be fully loaded to the client when the page is opened. The fluency mentioned here is a visual buffer during the waiting process. The following is a rendering of the Google Plus page:

The speed of accessing google in the wall is not very fast. Many of the content in the above elements are loaded asynchronously, and it can be seen from the above figure that Google does not make users feel anxious about waiting.

Performance optimization of Taobao homepage

由于平台限制,淘宝首页面临一个先天的性能缺陷,首屏的渲染需要从 7 个不同的后端取数据,这些数据请求是难以合并的,如果用户屏幕比较大,则首屏的面积也比较大,对应的后端平台数据接口就更多。数据是个性化内容或者为广告内容,故请求也不能缓存。

关键模块优先

不论用户首屏的面积有多大,保证关键模块优先加载。下面代码片段是初始化所有模块的核心部分:

TMS 输出的模块都会包含一个 .J_Module 钩子,并且会预先加载 js 和 css 文件。

对于无 JS 内容的模块,会预先打上 tb-pass 的标记,初始化的时候跳过此模块;对于首屏模块关键模块,会直接进入懒加载监控:

除必须立即加载的模块外,关键模块被加到懒加载监控,原因是,部分用户进入页面就可能急速往下拖拽页面,此时,没必要渲染这些首屏模块。

非关键模块统一送到 lazyQueue 队列,没有基于将非关键模块加入到懒加载监控,这里有两个原因:

  • 一旦加入监控,程序滚动就需要对每个模块做计算判断,模块太多,这里可能存在性能损失

  • 如果关键模块还没有加载好,非关键模块进入视窗就会开始渲染,这势必会影响关键模块的渲染

那么,什么时候开始加载非关键模块呢?

上面的代码应该十分清晰,两种请求下会开始将非关键模块加入懒加载监控:

  • 当页面中触发 mousemove scroll mousedown touchstart touchmove keydown resize onload 这些事件的时候,说明用户开始与页面交互了,程序必须开始加载。

  • 如果用户没有交互,但是页面已经 onload 了,程序当然不能浪费这个绝佳的空档机会,趁机加载内容;经测试,部分情况下,onload 事件没有触发(原因尚不知),所以还设定了一个超时加载,5s 之后,不论页面加载情况如何,都会将剩下的非关键模块加入到懒加载监控。

懒执行,有交互才执行

如果说上面的优化叫做懒加载,那么这里的优化可以称之为懒执行。

首页上有几个模块是包含交互的,如头条区域的 tab ,便民服务的浮层和主题市场的浮层,部分用户进入页面可能根本不会使用这些功能,所以程序上并没有对这些模块做彻底的初始化,而是等到用户 hover 到这个模块上再执行全部逻辑。

更懒的执行,刷新页面才执行

首屏中有两个次要请求,一个是主题市场的 hot 标,将用户最常逛的三个类目打标;第二个是个人中心的背景,不同的城市会展示不同的背景图片,这里需要请求拿到城市信息。

这两处的渲染策略都是,在程序的 idle(空闲)时期,或者 window.onload 十秒之后去请求,然后将请求的结果缓存到本地,当用户第二次访问淘宝首页时能够看到效果。这是一种更懒的执行,用户刷新页面才看得到.这种优化是产品能够接受,也是技术上合理的优化手段。

图片尺寸的控制和懒加载

不论图片链接的来源是运营填写还是接口输出,都难以保证图片具备恰当的宽高,加上如今 retina 的屏幕越来越多,对于这种用户也要提供优质的视觉体验,图片这块的处理并不轻松。

阿里 CDN 是支持对图片尺寸做压缩处理的,如下图为 200×200 尺寸的图片:

加上 _100x100.jpg 的参数后,会变成小尺寸:

我们知道 webp 格式的图片比对应的 jpg 要小三分之一,如上图加上 _.webp 参数后:

(不支持 webp 格式的浏览器展示不出来这张图片)

视觉效果并没有什么折扣,但是图片体积缩小了三分之一,图片越大,节省的越明显。显然,淘宝首页的所有图片都做了如上的限制,针对坑位大小对图片做压缩处理,只是这里需要注意的是,运营填写的图片可能已经是压缩过的,如:

上面这种情况,图片是不会正确展示的。首页对所有的图片的懒加载都做了统一的函数处理:

模块去钩子,走配置

TMS 的模块在输出的时候会将数据的 id 放在钩子上:

如果模块是异步展示的,可以通过 tms-datakey 找到模块数据,而首页的个性化是从几十上百个模块中通过算法选出几个,如果把这些模块钩子全部输出来,虽说取数据方便了很多,却存在大量的冗余,对此的优化策略是:将数据格式相同的模块单独拿出来,新建页面作为数据页。所以可以在源码中看到好几段这样的配置信息:

减少了大量的源码以及对 DOM 的解析。

低频修改模块,缓存请求

有一些模块数据是很少被修改的,比如接口的兜底数据、阿里 APP 模块数据等,可以通过调整参数,设置模块的缓存时间,如:

Math.floor(new Date / (1000 * 60)) 这个数值在一分钟内是不会发生变化的,也就是说将这个请求在本地缓存一分钟,对于低频修改模块,缓存时间可以设置为一天,即:

当然,我们也可以采用本地储存的方式缓存这个模块数据:

缓存过期时间设置为 1 天,淘宝首页主要采用本地缓存的方式。

使用缓动效果减少等待的焦急感

这方面的优化不是很多,但是也有一点效果,很多模块的展示并不是干巴巴的 .show(),而是通过动画效果,缓动呈现,这方面的优化推荐使用 CSS3 属性去控制,性能消耗会少很多。

优化的思考角度

上文 《一起来看看淘宝首页的个性化》 中提到几个黄金法则:

  • 首屏一定要快

  • 滚屏一定要流畅

  • 能不加载的先别加载

  • 能不执行的先别执行

  • 渐进展现、圆滑展现

性能优化的切入角度不仅仅是上几个方面,对照 Chrome 的 Timeline 柱状图和折线图,我们可以找到几个优化的点:

  • 在 1.0s 左右存在一次 painting 阻塞,可能因为一次性展示的模块面积过大

  • 从 FPS 的柱状图可以看出,在 1.5s-2.0s 之间,存在几次 Render 和 JavaScript 丢帧

  • 从多出的红点可以看出页面 jank 次数,也能够定位到代码堆栈

在优化的过程中需要更多地思考,如何让阻塞的脚本分批执行,如何将长时间执行的脚本均匀地分配到时间线上。这些优化都体现在代码的细节上,宏观上的处理难以有明显的效果。当然,在宏观上,淘宝首页也有一个明显的优化:

这段代码基本保证每个模块的初始化都是在浏览器空闲时期,减少了很多不必要的丢帧。这个优化也可以被应用到每个模块的细节代码之中,不过优化难度会更高。

小结

代码的性能优化是一个精细活,如果你要在一个庞大的未经优化的页面上做性能优化,可能会面临一次重构代码。本文从淘宝首页个性化引出的问题出发,从微观到宏观讲述了页面的优化实践,提出了几条可以借鉴的「黄金法则」,希望对你有所启发,后续会继续给大家带来淘宝首页稳定性保障的分享。

本文为转载文章,作者:小胡子哥(@Barret李靖) 网址:http://www.barretlee.com/blog/2016/04/01/optimization-in-taobao-homepage/,如有侵权请第一时间联系公众号:数通畅联。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326988075&siteId=291194637