Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Put javascript in one .js file or break it out into multiple .js files?

Tags:

javascript

My web application uses jQuery and some jQuery plugins (e.g. validation, autocomplete). I was wondering if I should stick them into one .js file so that it could be cached more easily, or break them out into separate files and only include the ones I need for a given page.

I should also mention that my concern is not only the time it takes to download the .js files but also how much the page slows down based on the contents of the .js file loaded. For example, adding the autocomplete plugin tends to slow down the response time by 100ms or so from my basic testing even when cached. My guess is that it has to scan through the elements in the DOM which causes this delay.

like image 699
Kevin Pang Avatar asked Feb 17 '09 06:02

Kevin Pang


People also ask

Should I put JavaScript in a separate file?

You should put your JS code in a separate file because this makes it easier to test and develop. The question of how you serve the code is a different matter. Serving the HTML and the JS separately has the advantage that a client can cache the JS.

Should all JavaScript be in one file?

It is generally accepted, that you should concat your scripts into one file to minimize requests and speedup pageload. Something most people don't know about, is that this rule was established before 2006 and browsers changed and improved a lot since than.


2 Answers

I think it depends how often they change. Let's take this example:

  • JQuery: change once a year
  • 3rd party plugins: change every 6 months
  • your custom code: change every week

If your custom code represents only 10% of the total code, you don't want the users to download the other 90% every week. You would split in at least 2 js: the JQuery + plugins, and your custom code. Now, if your custom code represents 90% of the full size, it makes more sense to put everything in one file.

When choosing how to combine JS files (and same for CSS), I balance:

  • relative size of the file
  • number of updates expected
like image 84
Julien Avatar answered Sep 23 '22 21:09

Julien


Common but relevant answer:

It depends on the project.

If you have a fairly limited website where most of the functionality is re-used across multiple sections of the site, it makes sense to put all your script into one file.

In several large web projects I've worked on, however, it has made more sense to put the common site-wide functionality into a single file and put the more section-specific functionality into their own files. (We're talking large script files here, for the behavior of several distinct web apps, all served under the same domain.)

The benefit to splitting up the script into separate files, is that you don't have to serve users unnecessary content and bandwidth that they aren't using. (For example, if they never visit "App A" on the website, they will never need the 100K of script for the "App A" section. But they would need the common site-wide functionality.)

The benefit to keeping the script under one file is simplicity. Fewer hits on the server. Fewer downloads for the user.

As usual, though, YMMV. There's no hard-and-fast rule. Do what makes most sense for your users based on their usage, and based on your project's structure.

like image 25
Matt Howell Avatar answered Sep 20 '22 21:09

Matt Howell