DescriptionDefer late and async scripts
Some background: the net loader will treat any Medium or High priority
resources discovered before the parser reaches the <body> tag as
critical and will block loading of lower priority resources until those
have finished loading. This includes resources discovered by the
preload scanner (see the bug for implications of this).
This patch adds logic to the preload scanner to teach it about the body
tag so that resources can be prioritized based on when and how they are
referenced, not just when they are discovered. It also changes the
prioritization of loading scripts and css:
- Scripts with Async or Defer attributes are now loaded with the same
priority as images (Very Low) since they do not block parsing or DOM
Content Loaded (DCL). Loading them with the same priority as images
gives sites some level of control over ordering where async scripts at
the top of the document would load earlier and async scripts at the end
would load later. Currently async scripts are treated like blocking
scripts and are treated as critical resources (blocking all images from
loading).
- Scripts injected dynamically by creating a script node and attaching
it to the document are treated like async scripts (and have the same
execution properties).
- Scripts written into the page with document.write are processed and
given the same priorities as normal parsed scripts (Medium priority).
- Scripts and CSS before the body tag are loaded at the same priority
(Medium) in the order they were discovered (since they can block each
other if they are interleaved). Previously CSS was given a higher
priority than scripts.
- Scripts and CSS after the body tag are loaded with a Low priority
which is higher than non-visible images but low enough that they are
not counted as critical resources for the loader. By loading them with
a higher priority than images we still get them loaded quickly and avoid
blocking the main parser (and delaying page interaction/DCL) while still
rendering sooner.
- Low-priority scripts that have not loaded by the time the main parser
reaches them are re-prioritized as Medium. Practically this should have
no impact since they are loaded in order anyway but it it a side-effect
of how prioritization is done and has no downside.
Desktop test results (some NSFW sites): https://docs.google.com/spreadsheet/ccc?key=0As3TLupYw2RedHNsUUd4dWNDS2RuaHpzVy05UDR0QlE&usp=sharing
In testing this improves the metrics across the board:
~3% on Speed Index
~5% on start render
~3% on DOM Content Loaded
~6% on onload
Getting out of the critical phase while still loading the scripts before
non-visible images are largely why we could improve the visible metrics
without degrading the DCL and onload metrics.
BUG=317785
Patch Set 1 : Lower priorities for late/async scripts #Messages
Total messages: 7 (1 generated)
|