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: https://csstriggers.com/

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 http://gent.ilcore.com/2011/03/how-not-to-trigger-layout-in-webkit.html, 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. Example site

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!

[Preference] How to avoid Forced Synchronous Layout or FSL to improve site preference的更多相关文章

  1. 性能优化-FSL(Force Synchronous Layout)强制同步布局

    通过chrome的Perfermance工具记录程序性能,切换到帧模式,点开其中一帧,看详情,中间为紫色的区块代表Layout,右上角带有红色三角的为警告,是chrome告知的强制同步布局,即FSL. ...

  2. 【译】使用requestIdleCallback

    原文地址:http://galen-yip.com/2015/10/07/%E3%80%90%E8%AF%91%E3%80%91%E4%BD%BF%E7%94%A8requestIdleCallbac ...

  3. 提高scroll性能

    在DevTools中开始渲染,向下滑动一点点滚动条,然后停止滚动. 在结果中,注意frames总是在30ftps线上面,甚至都木有很接近69ftps线的(事实上帧执行的太缓慢以致于60ftps线在图上 ...

  4. Should I expose synchronous wrappers for asynchronous methods?

    In a previous post Should I expose asynchronous wrappers for synchronous methods?, I discussed " ...

  5. Preference如何增加在activity生命周期监听器

    转载请注明出处:http://blog.csdn.net/droyon/article/details/41313115 本文主要介绍Preference凭什么Activit一些逻辑的生命周期,使. ...

  6. Android Preference详解

    转载请标明出处:ttp://blog.csdn.net/sk719887916/article/details/42437253 Preference 用来管理应用程序的偏好设置和保证使用这些的每个应 ...

  7. Android偏好设置(7)自定义Preference,和PreferenceDialog

    Building a Custom Preference The Android framework includes a variety of Preference subclasses that ...

  8. Android Preference使用

    Android Preference经常使用在例如设置的功能,Android提供preference这个键值对的方式来处理这种情况,自动保存这些数据,并立时生效,这种就是使用android share ...

  9. Android中Preference的使用以及监听事件分析

    在Android系统源码中,绝大多数应用程序的UI布局采用了Preference的布局结构,而不是我们平时在模拟器中构建应用程序时使用的View布局结构,例如,Setting模块中布局.当然,凡事都有 ...

随机推荐

  1. webpack到底怎么用?

    webpack到底怎么用? https://www.zhihu.com/question/39290543

  2. MyBatis的关联映射和动态SQL

    CREATE TABLE tb_card ( id INT PRIMARY KEY AUTO_INCREMENT, CODE ) ); '); CREATE TABLE tb_person ( id ...

  3. AWS S3 对象存储服务

    虽然亚马逊云非常牛逼,虽然亚马逊云财大气粗,虽然亚马逊用的人也非常多,可是这个文档我简直无法接受,特别是客服,令人发指的回复速度,瞬间让人无语,可是毕竟牛逼.忍了,躺一次坑而已 1.图片上传 1.1 ...

  4. F - Dima and Lisa(哥德巴赫猜想)

    Problem description Dima loves representing an odd number as the sum of multiple primes, and Lisa lo ...

  5. jbox如果弹不出,放在body里

    body> <form id="form1" runat="server"> <script type="text/javas ...

  6. Linux运维最佳实践之网站调优

    高性能静态网站: 1.静态页面中针对图片进行浏览器(客户端)缓存,如公共JavaScript(jQuery,jQuery-1.12.1.min.js)进行缓存 2.对网站输入内容压缩(gzip) 3. ...

  7. Android View事件分发与传递

    在Android中,人们主要通过手指与系统交互.Android把所有的touch事件都被封装成MotionEvent来进行处理,其中包括了手指点击的位置,时间等信息.其事件类型主要包括:ACTION_ ...

  8. art-template简单使用

    art-template是一款较通用的前端模板引擎. 简单的使用方法如下: 具备3个要素 1)模板 <script type="text/template" id=" ...

  9. jq+mui 阻止事件冒泡

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  10. MySQL定时任务与存储过程实例

    shell 定时任务:​/usr/bin/mysql -uroot -pxxxxx databasename -e "update table set ......."​​mysq ...