2

Also, I know css and image files can be downloaded in parallel. But can javascript files be downloaded in parallel? Thanks.

Bobo
  • 8,777
  • 18
  • 66
  • 85

2 Answers2

3

When the HTML parser sees a script tag, barring your using a couple of special attributes, it comes to a screeching halt, downloads the JavaScript file, and then hands the contents off to the JavaScript interpreter. Consequently, script files cannot be downloaded in parallel either with each other or with the main HTML...unless you use the defer or async attributes and the browser supports them. Details in this other answer here on StackOverflow.

Note that even if you have multiple resources that can be downloaded in parallel, there's no guarantee that they will be. Browsers (and servers) put limits on the number of simultaneous connections between the same two endpoints. (With modern browsers the limit is usually at least four — up from two — but browsers may dial things down on dial-up connections and on mobile devices; and of course, they're entirely free to only use a single connection, it's implementation-specific).

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • 1
    I like the answer. However, it's not that "script files cannot be downloaded in parallel" (a browser could optimistically perform this task; there are some issues with document modifications from the executed scripts, etc -- hence just being "optimistic") rather, it is that "scripts must execute in sequence" (and in-flow with the document). –  Dec 17 '10 at 19:27
  • @pst: A **very** good point. Browsers *could* do that. As far as I know, none does, but I could just be ignorant of a specific optimization. Even if none does at present, though, you're absolutely right. – T.J. Crowder Dec 17 '10 at 22:28
0

Scripts are loaded in parallel in modern browsers and IE8. Firefox and Safari will download as many as six scripts at once. IE8 however seems to not have a limit set - my tests showed as many as 18 scripts load at once.

However, whether it's one script or six, everything else is blocked. So the browser will not load one script and one style sheet.

You can get a good idea of the flow by looking at Firebug in the Net tab. Those bars you see on the right are called a waterfall chart, and gives you a reasonably accurate representation of when things load and how long they take.

You can learn more about page loading in this presentation: Optimize Your Website to Load Faster The stuff on resource loading starts on slide 33.

mwilcox
  • 4,065
  • 23
  • 20