Measuring Script Load Performance With the Navigation Timing API
Each of these pages loads 5 scripts. One at the top, one at the bottom, one using async
, one using defer
and one with a simulated 1s delay. We experiment with different ways of loading the slow script and see how they affect the loading of the other scripts.
- The first two examples ares the only ones where the slow script is actually executed last!
- The Async JS example uses the 3rd party script loading example described in this post: Async script loading of 3rd party JS.
- Don't bother comparing the numbers too much, the iframe loading is probably screwing things up a bit.
timeToScript
below is Date.now() - performance.timing.navigationStart
.
- Putting the slow script at the bottom still means any scripts below it have to wait!
- Using
defer
on a slow script is similar to putting it at the bottom of the page.
- In the slowtop example the first script on the page doesn't have access to the DOM when the 2nd script blocks for 1 second (see the console.)