Search code examples
htmlpagespeed

How much slower is it to put .js in an external file rather than directly on the page?


I assume that if you put some Javascript code in an external source (and use the src="") that it's a little slower b/c the page has to then download another portion, but I'm wondering whether that's inconsequential.

From testing I've done online (with webpagetest.org) seems quite small (< 5% of the total page time loading).

But just wondering about what's happening "under the hood" and whether the browser (I assume) is spinning up another process to download that bit separately rather than coming across from the server with the rest of the page is actually just as fast (b/c it's happening in parallel).


Solution

  • Not slow enough to matter.

    I think the speed difference question is a red herring. Generally, you should keep your script separate from your html:

    • Separation of concerns: the html is the structure of the site, whereas the script is its behavior. It mixes concerns to mingle them together, and it's best practice to keep your script in a separate file.
    • It might seem counter-intuitive that script served separately from html could be just as fast or even faster, but things like caching proxy servers, content delivery networks, and even new web protocols like SPDY can make the speed question completely moot.