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

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

Leave a Comment