Browser script loading roundup

February 7, 2010 12:12 am | 8 Comments

How are browsers doing when it comes to parallel script loading?

Back in the days of IE7 and Firefox 2.0, no browser loaded scripts in parallel with other resources. Instead, these older browsers would block all subsequent resource requests until the script was received, parsed, and executed. Here’s how the HTTP requests look when this blocking occurs in older browsers:

The test page that generated this waterfall chart has six HTTP requests:

  1. the HTML document
  2. the 1st script – 2 seconds to download, 2 seconds to execute
  3. the 2nd script – 2 seconds to download, 2 seconds to execute
  4. an image – 1 second to download
  5. a stylesheet- 1 second to download
  6. an iframe – 1 second to download

The figure above shows how the scripts block each other and block the image, stylesheet, and iframe, as well. The image, stylesheet, and iframe download in parallel with each other, but not until the scripts are finished downloading sequentially.

The likely reason scripts were downloaded sequentially in older browsers was to preserve execution order. This is critical when code in the 2nd script depends on symbols defined in the 1st script. Preserving execution order avoids undefined symbol errors. But the missed opportunity is obvious – while the browser is downloading the first script and guaranteeing to execute it first, it could be downloading the other four resources in parallel.

Thankfully, newer browsers now load scripts in parallel!

This is a big win for today’s web apps that often contain 100K+ of JavaScript split across multiple files. Loading the same test page in IE8, Firefox 3.6, Chrome 4, and Safari 4 produces an HTTP waterfall chart like this:

Things look a lot better, but not as good as they should be. In this case, IE8 loads the two scripts and stylesheet in parallel, but the image and iframe are blocked. All of the newer browsers have similar limitations with regard to the extent to which they load scripts in parallel with other types of resources. This table from Browserscope shows where we are and the progress made to get to this point. The recently added “Compare” button added to Browserscope made it easy to generate this historical view.

While downloading scripts, IE8 still blocks on images and iframes. Chrome 4, Firefox 3.6, and Safari 4 block on iframes. Opera 10.10 blocks on all resource types. I’m confident parallel script loading will continue to improve based on the great progress made in the last batch of browsers. Let’s keep our eyes on the next browsers to see if things improve even more.

8 Responses to Browser script loading roundup

  1. Hi ! I think this is very important, even if more and more efforts are made to launch JS at the bottom or to combine all the scripts in only one.

    I have a little question : do you have some doc on how browsers behave in general? For example, is it more interesting to include an image with width and height in the CSS file or in the HTML tag itself?

    Thank you (and also for rock music!).

  2. opera 10.5 ;)
    || script script = yes
    || script Stylesheet = yes
    || script Image = yes
    || script Iframe = yes

  3. See if we could just get rid of document.write then it would work fine. You could run scripts without blocking the page.

    Of course all the ad scripts would get very confused.

  4. Thank you very much for that information, the comparison is very interesting and the Browserscope website is a very cool resource.

  5. Opera has a config setting to allow parallel loading of scripts:
    opera:config#Extensions|DelayedScriptExecution

    It’s off by default, but enabling it gives the score as indicated by Gleb for Opera 10.10 as well.

    I think you’re statement that Opera 10.10 blocks on all resource types, is therefore not completely right.

  6. @Janghou: Yes, you can read more about Opera and their support for deferring scripts that do document.write here: http://www.stevesouders.com/blog/2008/09/11/delayed-script-execution-in-opera/

    However, since this is off by default and documentation says “Primarily for low bandwidth devices, not well-tested on desktop” it’s fairer to say Opera doesn’t support parallel script loading.

  7. Is there any way to force the newer browser versions to disable parallel script loading?
    I have a webpage that I use for performance testing that loads several small scripts and then one large, taking timings for both the small set and the large file, I need them to all use the same TCP session. (opens the TCP congestion window and then slams the network with 64KB)This works for IE7 but not FF3.5

  8. @Steve: So Opera invented it, but doesn’t use it by default. Strange guys those Norwegians.

    The Opera documentation you’re referring to is quite old. < 9.6*

    The explanation Hallvord Steen gave in your comments against enabling it by default, http://www.stevesouders.com/blog/2008/09/11/delayed-script-execution-in-opera/#comment-255 should be applicable to other browsers as well.
    Are you aware of any real world examples in which parallel loading actually breaks the website as Hallvord pointed out the danger for and which is a theoretical possibility not only in Opera?
    Have you seen it break in Opera and work in another browser?
    When that's not the case, it would say Opera does support it, but doesn't enable it by default, because theoretically it can break (badly designed) websites.