How a web page loads

The major web browsers load web pages in basically the same way. This process is known as parsing and is described by the HTML5 specification. A high-level understanding of this process is critical to writing web pages that load efficiently.

Parsing overview

As chunks of the HTML source become available from the network (or cache, filesystem, etc), they are streamed to the HTML parser. Next, in a process known as tokenization, the parser iterates through the source generating a token for (most notably) each start tag, end tag and character outside of a tag.

For example the input source <b>hello</b> yields 7 tokens:

start-tag { name: b }
character { data: h }
character { data: e }
character { data: l }
character { data: l }
character { data: o }
end-tag { name: b }

After each token is generated it is serially passed to the next major subsystem: the tree builder. The tree builder dynamically modifies the Document‘s DOM tree to reflect the new token.

The 7 input tokens above yield the following DOM tree:

<html>
  <head>
  <body>
    <b>
      "hello"

Fetching subresources

A frequent operation performed by the tree builder is creating a new HTML element and inserting it into the Document. It is at the point of insertion that HTML elements which load subresources begin fetching the subresource.

Running scripts

This parsing algorithm seems to translate HTML source into a DOM tree as efficiently as possible. That is, except for one wrinkle: scripts. When the tree builder encounters an end-tag token for a script, it must serially execute the script before parsing can continue (unless the associated script start-tag has the defer or async attribute).

There are two significant preconditions which must be fulfilled before a script can execute:

  1. If the script is external its source must be fully downloaded.
  2. For any script, all stylesheets in the document must be fully downloaded.

This means often the parser must idly wait while scripts and stylesheets are downloaded.

Why must parsing halt?

Well, a script may document.write something which affects further parsing or it may query something about the DOM which would yield incorrect results if parsing had continued (for instance the number of image elements in the DOM).

Why wait for stylesheets?

A script may expect to access the CSSOM directly or it may query an attribute of a DOM node which depends on the stylesheet (for example, how wide is a certain <table>).

Is it inefficient to block parsing?

Yes. Subresource download times often have a large constant factor limited by round trip time. This means it is faster to download two resources in parallel than to download the same two in serial. More obviously, the browser is also free to do CPU work while waiting on the network. For these reasons it is critical to efficient loading of a web page that subresource fetches are initiated as soon as possible. When parsing is blocked, the tree builder is not able to insert subsequent elements into the DOM, and thus subsequent subresource downloads are not initiated even if the HTML source which includes them is already available to the parser.

Mitigating blocking

As I‘ve blogged previously, when the parser becomes blocked WebKit will run a lightweight parser known as the preload scanner. It mitigates the blocking problem by scanning ahead and fetching certain subresource that may be required. Other browsers employ similar techniques.

It is important to note that even with preload scanning, parsing is still blocked. Nodes cannot be added to the DOM tree. Although I haven‘t covered how a DOM tree becomes a render tree, layout or painting, it should be obvious that before a node is in the DOM tree it cannot be painted to the screen.

Finishing parsing

After the entire source has been parsed, first all deferred scripts will be executed (waiting for their source and all pending stylesheets to download). Their completion triggers the DOMContentLoaded event to be fired. Next, the parser will wait for any pending async scripts to finish loading and executing. Finally, once all subresources have finished downloading, the window‘s load event will be fired and parsing is complete.

Takeaway

With this understanding, it becomes clear how important it is to carefully consider where and how stylesheets and scripts are included in the document. Those decisions can have a significant impact on the efficiency of the page load.

How a web page loads,布布扣,bubuko.com

时间: 2024-10-10 10:54:05

How a web page loads的相关文章

解读Web Page Diagnostics网页细分图

http://blog.sina.com.cn/s/blog_62b8fc330100red5.html Web Page Diagnostics (以下简称WPD),这是LR Analysis中非常重要的一块,搞清楚这部分的内容会让你少走很多弯路,很多环境问题都可以通过它来定位,比如客户端,网络.通过它可以你可以比较好的来定位是环境的问题还是应用本身的问题,当然更重要的是Web页面本身的问题. Web Page Diagnostics:页面诊断图,也叫页面分解总图 “页面分解”显示某一具体事务

Web Page Diagnostics网页细分图解读

Web Page Diagnostics (以下简称WPD),这是LR Analysis中非常重要的一块,搞清楚这部分的内容会让你少走很多弯路,很多环境问题都可以通过它来定位,比如客户端,网络.通过它可以你可以比较好的来定位是环境的问题还是应用本身的问题,当然更重要的是Web页面本身的问题. Web Page Diagnostics:页面诊断图,也叫页面分解总图 “页面分解”显示某一具体事务在测试过程的响应情况,进而分析相关的事务运行是否正常. 此视图下面的文件结构大体上与scripts中act

New JavaScript techniques for rapid page loads 加快页面载入的新js技术

Speed has always been one of Chrome's primary missions, ever since it was included as one of the founding principles in 2008. But speed is about more than just traditional Javascript benchmarks. Ideally every part of a user's interaction with a brows

调试android chrome web page简明备忘

必备工具 adb tools.android chrome 先开启手机调试模式 adb forward tcp:9919 localabstract:chromedevtoolsremote 成功会提示 * daemon not running. starting it now on port 5037 * * daemon started successfully * 不成功的话用这个命令再试 adb kill-server 接着打开下面的链接,这里的9919是上面adb命令中指定的 http

LR实战之Discuz开源论坛——网页细分图结果分析(Web Page Diagnostics)

续LR实战之Discuz开源论坛项目,之前一直是创建虚拟用户脚本(Virtual User Generator)和场景(Controller),现在,终于到了LoadRunner性能测试结果分析(Analysis)这部分了. LoadRunner结果分析图表功能中最重要图表分析之一,就是网页诊断细分图,在Controller场景设计运行之前,需要在菜单栏中设置启用网页诊断功能(诊断-网页诊断-启动),如图: 网页细分图,是显示每个页面及其组件的相关下载时间和大小,主要用来评估页面内容是否影响事务

Lesson 2 Building your first web page: Part 3

Time to build your first HTML page by hand I could go on with more theory and send half of you to sleep (trust me); instead you are now going to actually build your very first web page! One of the best ways to learn something is to actually do it, so

微信(WeChat web page)

插件介绍: 说到微信,相信身边的很多朋友都在使用这个软件,因为它有着很多强大的功能,包括朋友圈.摇一摇.漂流瓶.语音提示等,使之成为我们生活中很重要的部分,最初在手机上使用,后来也出现了网页版,今天为大家推荐这款网页版的微信插件. 使用说明: 将微信(WeChat web page)添加至chrome,并在应用中启动它. 功能介绍: -通讯录-朋友圈-发送文件-截图工具-桌面通知-公共账号 http://www.chrome001.com/谷歌插件大全长期为您更新.

Lesson 2 Building your first web page: Part 2

Tag Diagram You may have noticed that HTML tags come in pairs; HTML has both an opening tag (<tag name>) and a closing tag (</tag name>). The only difference between the opening and closing tags is that the closing tag contains an extra forwar

Lesson 2 Building your first web page: Part 1

In this ‘hands-on’ module we will be building our first web page in no time. We just need to quickly cover a couple of points beforehand to help get our feet on the ground. The three ways you can build a web page 1. Use a pre-made template: WHAT IS A