views:

115

answers:

7

Is it bad practice to have a single javascript file that gets loaded accross all pages even if certain functions are only needed on certain pages? Or should the files be split up according to functionality on a given page and loaded only by pages that need them?

+1  A: 

If this file is really large, it could impact certain user's perceived performance (i.e. download and render times). IMHO you should split it up into reasonable groups of functions, with each group having similar functions (such that pages only reference the files they need).

Matthew Jones
A: 

depends on the size and complexity of the unused functions.

the javascript-parser anyway only stores the location and the signature of each function. as far as i know, it is only parsed when executed.

if traffic is a problem for you, rather include only those you need...

regards

Atmocreations
A: 

Since the JS files are cached once they are downloaded and the JS parser shows no noticable performance difference btw a big JS file(not a HUGE one ;)) and a small js file, you should go with the single file approach.

Also it is known that multiple js files reduces the performance.

BYK
A: 

You're best off with a single JS file, as browsers will cache it after the first request for it (unless they've turned that off, in which case they deserve what they get). Another thing that will vastly, vastly increase your perceived performance on page load is turning on gzip compression in the web server - most JS files compress very well.

DDaviesBrackett
+1  A: 

According to YSlow less files is better, but try to keep each file under 25k. Also make sure you minify or otherwise reduce the size of the js (and css) files. If possible turn on GZip for js on the webserver and set a far future expires header.

See here for the Yahoo Developer performance best practices.

Frozenskys
A: 

I would recommend to use one big file, for each file the browser launches a web request. Most browsers, I'm not quite sure how much it is with the newest versions of the known browsers, only launch a few concurrent web requests. The browser will wait until the files have been downloaded before launching the next web requests. The one big file will be cached and other pages will load faster.

As @Frozenskys mentions YSlow states that less files is better, one of the major performance enhancements proposed by the Yahoo team is to minimize the amount of http requests.

Of course if you have a HUGE javascript file that literally takes seconds to download, it's better to split it up to prevent that the user has to wait seconds before the page loads.

PeterEysermans
A: 

A single file means a single download; as this article explains, most browsers will only allow a limited number of parallel requests to a single domain. Although your single file will be bigger than multiple small ones, as the other answers have pointed out:

  1. The file will be cached
  2. Techniques like minification and server-side gzip compression will help to reduce the download time.

You can also include the script at the end of the page to improve the perceived load time.

Ken Keenan