Split the Initial Payload

[This post is based on a chapter from my next book, a follow-up to High Performance Web Sites containing more best practices for faster web sites.]

The growing adoption of Ajax and DHTML means today’s web pages have more JavaScript than ever before. The average top ten U.S. web site[1] contains 252K of JavaScript. JavaScript slows pages down. When the browser starts downloading or executing JavaScript it won’t start any other parallel downloads. Also, anything below an external script is not rendered until the script is completely downloaded and executed. Even in the case where external scripts are cached the execution time can still slow down the user experience and thwart progressive rendering.

Every line of JavaScript code matters to a fast loading page. And yet, as shown in Table 1, the average top ten U.S. web site only executes 25% of the JavaScript functionality before the onload event.[2] Why track this relative to the onload event? Any code needed after the onload event (for dropdown menus, Ajax requests, etc.) could be downloaded later, making the initial page render more quickly.

Table 1. Percentage of JavaScript functions executed before onload

Web Site % of Functions
Executed
JavaScript Size
(uncompressed)
http://www.aol.com/ 29% 115K
http://www.ebay.com/ 44% 183K
http://www.facebook.com/ 9% 1088K
http://www.google.com/search?q=flowers 44% 15K
http://search.live.com/results.aspx?q=flowers 25% 17K
http://www.msn.com/ 31% 131K
http://www.myspace.com/ 13% 297K
http://en.wikipedia.org/wiki/Flowers 21% 114K
http://www.yahoo.com/ 12% 321K
http://www.youtube.com/ 16% 240K

The task of finding where to split a large set of JavaScript code is not trivial. Doloto, a project from Microsoft Research, attempts to automate this work. Doloto is not publicly available, but the paper provides a good description of their system. (You can here the creators talk about Doloto at the upcoming Velocity conference.) The approach taken by Doloto uses stub functions that download additional JavaScript on demand. This might result in users having to wait when they trigger an action that requires additional functionality. Downloading the additional JavaScript immediately after the page has rendered might result in an even faster page.

[1] These top sites were extracted from Alexa.

[2] The percentage of functions executed is based on dividing the number of executed functions reported by Firefox’s JavaScript debugger by the total number of functions at the time of the onload event.