博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Preference] How to avoid Forced Synchronous Layout or FSL to improve site preference
阅读量:6266 次
发布时间:2019-06-22

本文共 2179 字,大约阅读时间需要 7 分钟。

When tigger site updates the layout, it always follow this order:

 

Javascript trigger style changes, then layout changes then broswer do the paint and composite

All those five steps should be finished in 60fps, or 16ms. Then users will have a smooth and good user experience.

 

For "layout", "style" and "composite", please check this site: 

From the site, you can see that, 'transform' and 'opacity' has good preference, because they only trigger "composite", save lot of works for the broswer.

 

 

Also you can see that the method for "left", "right", they triggers all "layout", "paint", "composite"

 

Now let see "style" and "layout".

In general, "style" should happen before "layout", otherwise, broswer need to rerender "layout"->"style"->"layout" all over again, which is a waste for the perfermence.

To see which opreation will cause "layout" recalcuation, please checkout , basiclly, be careful with those:

clientHeight, clientLeft, clientTop, clientWidth, focus(), getBoundingClientRect(), getClientRects(), innerText, offsetHeight, offsetLeft, offsetParent, offsetTop, offsetWidth, outerText, scrollByLines(), scrollByPages(), scrollHeight, scrollIntoView(), scrollIntoViewIfNeeded(), scrollLeft, scrollTop, scrollWidth

 

Let's see an example how bad it can affect our site prefermence.

In this bad example, you can see taht Recalculation for "style" -> "layout" -> "style" .... "layout", it repeat number of times.

 

 

 Let's see the code causes this:

function firstRun() {          divs.forEach(function(elem, index, arr) {            if (window.scrollY < 200) {              elem.style.opacity = 0.5;            }          })        }

As you can see in a forEach loop, every time you call "scollY" will cause a layout update, then we call "style.opacity" to trigger style update, but after style updated, layout will be updated again because the order, remember?

 

Let's see how to fix the problem:

function thirdRun() {          var newWidth = container.offsetWidth;          divs.forEach(function(elem, index, arr) {            elem.style.width = newWidth + "px";          })        }

We can simply move 'layout' update code out of forEach loop. Now the timeline looks much better!

 

 

转载地址:http://hjdpa.baihongyu.com/

你可能感兴趣的文章
js之广告弹出自动关闭
查看>>
axios请求requestBody和formData
查看>>
PSQL_标准API和Interface基本的用法和比较(概念)
查看>>
网站目录
查看>>
APUE-文件和目录(七)符号链接
查看>>
CSS 简介
查看>>
System Verilog基础(二)
查看>>
2018/11/26 Samba服务器配置
查看>>
2018/12/08 PAT刷题 L1-034 点赞
查看>>
如何改变TextBox.PassWordChar的值 转
查看>>
css的工作原理
查看>>
【pip】的安装
查看>>
内存泄漏及其检测工具
查看>>
QT Model based vs Item based
查看>>
[Leetcode]669 Trim a Binary Search Tree
查看>>
Linux C Programing - Arguments(2)
查看>>
禁止选择文本和禁用右键 v1.0
查看>>
swift 动画
查看>>
can 驱动
查看>>
Linux使用ASF云挂卡(挂游戏时长)
查看>>