资讯专栏INFORMATION COLUMN

Five minutes to understand async and defer

Alex / 530人阅读

摘要:,,

Script tag

When we want to insert a script into a web page, the standard way is to use the script tag(i.e.

hello world

The console output:

script1 is running at 1496747869008
script1.js:4 script1 element null
script1.js:8 script1 finishes running at 1496747870008
script2.js:2 script2 is running at 1496747870009
script2.js:4 script2 element null
script2.js:8 script2 finishes running at 1496747872009

Conclusion:

When we open the html in the browser, we can notice the delay of page load. The page goes blank before it renders correctly. This is due to the fact that the running of the two scripts blocks the DOM rendering.

When scripts are running, they are not able to fetch the DOM element (i.e. document.getElementById("load-experiment") being null). This is because scripts are run before DOM is rendered.

Scripts themselves are blocking each other. They are run in the order specified in the html. Script2 is run after script1 finishes running.

Put script tags at the bottom of body

This is the suggestion from the Rule 6 of the book High Performance Web Sites.



    
         test js tag async and defer attributes
    
    
        

hello world

The console output:

script1 is running at 1496751597679
script1.js:4 script1 element 

​ hello world ​

​ script1.js:8 script1 finishes running at 1496751598679 script2.js:2 script2 is running at 1496751598680 script2.js:4 script2 element

​ hello world ​

​ script2.js:8 script2 finishes running at 1496751600680

Conclusion:

No page delay and blank page when opening the page in browser, as scripts are put/run after the DOM is ready.

Scripts can correctly fetch DOM elements.

Scripts themselves are blocking each other, as the same as the first example.

However, puting all scripts at the bottom of body sometimes doesn"t fit in some specific real life cases. For example, if we aim to calculate the ATF[https://en.wikipedia.org/wiki...] of a page, we can"t simple wait for the DOM to render. We have to load and run some scripts beforehand and then fire the ATF marker when the ATF is ready, which is definitely before DOM is ready for web pages with scrolling content. Therefore, it seems reasonable to come out with the next solution.

Put scripts seperately in head and body based on requirements.

Put scripts that needs pre-running at head and others at the bottom of body. E.g.

 
     
         
    
defer!

The main disadvantage of putting scripts at the bottom of body is that the scripts will only be retrieved/loaded after the DOM is rendered. As we said, retrieving/loading the script content is NON-BLOCKING while running the script content is the BLOCKING part. We can improve the web performance if we can retrieve/load the scripts while the DOM is rendering, rather than wait for the DOM to complete rendering. This works pretty well especially when the scripts are large. This is why defer is introduced. defer loads the scripts simultaneously but only runs the scripts after the DOM is ready.



    
         test js tag async and defer attributes
        
        
    
    
        

hello world

The console output:

script1 is running at 1496760312686
script1.js:4 script1 element 

​ hello world ​

​ script1.js:8 script1 finishes running at 1496760313686 script2.js:2 script2 is running at 1496760313686 script2.js:4 script2 element

​ hello world ​

​ script2.js:8 script2 finishes running at 1496760315686

Conclusion:

The result is the same as putting scripts at the bottom of body. We can conclude from the result that the scripts are run after the DOM is ready as we can indeed fetch the DOM elements.

Even the defered scripts follow the order rule specified in html, script1 is run after script2.

async!

Web pages often contain some add-on features which are strictly independently and NOT must-to-have, such as the comment and chat functionalities on some pages. As the features are independent, they don"t have the running order restriction. In this case, we can use async



    
         test js tag async and defer attributes
    
    
        

hello world

We can observe different console outputs when we refresh the page:

The running order of script1 and script2 varies

The result of fetching DOM element is inconsistent

As async scripts don"t guarantee the running order, this is often the source of potential hidden bugs. Have a second thought before using async and mare sure these scripts are strictly independent.

Conclusion

The general rule to import script is:

 
     
        
         
     
    
        
        

hello world

Code Sample

Reference

Async JavaScript

High Performance Web Sites

Notice

If you benefit from this Repo,Please「Star 」to Support.

If you want to follow the latest news/articles for the series of reading notes, Please 「Watch」to Subscribe.

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/51966.html

相关文章

  • Five minutes to understand async and defer

    摘要:,, Script tag When we want to insert a script into a web page, the standard way is to use the script tag(i.e. ). The first impression that comes to peoples mind about script tag is - BLOCKING ....

    Chiclaim 评论0 收藏0
  • promise, async, await, execution order

    摘要: async can be transformed to promise. So, if we want to understand async, we have to understand promise first. Promise Normally, promise is easy to understand, especially when using like this: p...

    neuSnail 评论0 收藏0
  • 2017-07-11 前端日报

    摘要:前端日报精选月份前端资源分享第期打包实战上大漠穷秋全面解读核心特性拖放什么是装饰器应该在什么时候使用装饰器中文全栈工程师的自我修养浓缩笔记下做工程师不做码农常用布局简洁解决方案白底黑字进阶试试酷炫的视角技术周刊期知乎专栏实践当 2017-07-11 前端日报 精选 7月份前端资源分享【第992期】webpack 2 打包实战(上)大漠穷秋:全面解读Angular 4.0核心特性HTML...

    _ivan 评论0 收藏0
  • DOM中的各种区别小节

    摘要:欢迎光临小弟博客我的博客原文中的各种区别小节参考普通添加事件和事件绑定的事件监听与捕获和冒泡和的区别 相信大家在DOM的实际开发与学习过程中,肯定也遇到不少需要比较的东西,这里我主要列比较以下几点,更多的区别和总结,希望想到和遇到的朋友给我留言哦。 clientHeight/scrollHeight/offsetHeight defer vs async 事件模型-捕获/目标/冒泡...

    Guakin_Huang 评论0 收藏0
  • Async and Defer

    async and defer async   There are actually two ways we can bypass the problem of the blocking script — async and defer.   Async scripts will download the script without blocking rendering the page and...

    JackJiang 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<