views:

34

answers:

4

Would the page load faster if I keep all my JavaScript files in a separate file say MyCode.js and reference that file? Or does it not really matter?

Thanks in advanced!

+2  A: 

It very rarely matters for speed purposes unless you're Google. However, for readability and maintainability, it's much better to keep your JavaScript in external files.

If you put your <script> tags at the end of the page (just before the closing <body> tag) the JavaScript won't block loading of the page.

Skilldrick
A: 

Depending on JS code size. If You have huge JS code part then putting everything separately has few pluses:

  • You can split different file types among different domain names which can increase page load time - while most of browsers limit to 4 concurrent connections to the same domain.

  • It is easier to maintain

Rafal Ziolkowski
+1  A: 

If you've got plenty of scripts, there might be some to be saved by keeping js in external files as it allows the browser to do a better job of caching your script files. Same goes for CSS.

But generally, meh. The obvious advantage of externalising is maintainability.

Alexander Sagen
A: 

There are benefits for both inline and included script files.

Inline scripts get loaded with the HTML therefore only one HTTP request is required to download both this saves time, the resulting HTML file will be bigger but you do not waste time with the handshake and headers.

Included scripts get loaded as a separate HTTP request and therefore there is this overhead, however, included scripts get cached by the browser which means subsequent pages do not need to request this resource from the server again.

As a rule, include inline scripts wherever those scripts directly relate to the content of the page that is being requested, you will save HTTP requests for the page and you do not need to cache the scripts separately. For site wide scripts, always include these as separate script files, these will get downloaded once the first time they are needed and then a cached copy will be used for subsequent pages.

Stuie Wakefield