Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it better to minify javascript into a single bundle for the whole site, or a specific-to-each-page bundle?

Tags:

When minifying JavaScripts together in web-development, is it better from the user-loading-time point of view to:

  1. make one single big bundle of JavaScript containing all the script, and include this on each page - so each page will probably not need all of it, but once the user has it cached, they don't need to get any further scripts (until it expires from their cache, of course) - optimising for number-of-requests
  2. make one bundle of JavaScript per page, so that each page loads just the script that it needs and nothing else - so each page when first loaded will definitely require a JS request (but still subsequently have that cached. Optimising for size-of-requests.

I'm interested in some data upon which to base the decision for which strategy to go with. I can arrive at conclusions based on anecdote as easily as everyone else :-)

like image 854
Peter Mounce Avatar asked Sep 08 '10 10:09

Peter Mounce


People also ask

Should I put all my JavaScript in one file?

To avoid multiple server requests, group your JavaScript files into one. Whatever you use for performance, try to minify JavaScript to improve the load time of the web page. If you are using single page application, then group all the scripts in a single file.

Should you minify your JS?

Despite being an excellent programming language, JavaScript impacts web pages by slowing them down. To regain space and improve your page load speed, you must minify the JavaScript code. The minified version of JavaScript code can reduce the file size by as much as 30–90%.

Why should you minify your JavaScript scripts before publishing them?

The sole benefit of minified JavaScript code is allowing a client to download fewer bytes, enabling the page to load faster, use less battery, use less of a mobile data plan, etc. This is usually be done as a build step when releasing code to a web server. Many tools, like uglify for example, exist to do this for you.

Does bundle size affect performance?

How does bundle size affect performance? Large amounts of JavaScript negatively affect site speed in two distinct phases: During page load: big bundles take longer to download. During parse and compile: big bundles take longer to be turned into machine code, which delays JS initialisation.


1 Answers

It really depends on the sizes and functions of the script. It's common to have a single master.js for all your pages, which contains all the functionality required by every page of your site, whilst having other js files for functionality that might only be needed on certain pages.

Take Stack Overflow, for instance. They have a master.js file included on every page of the site, but when you visit a question page or the "ask a question" page you'll notice wmd.js. This script includes all the functionality for the editor which is needed on fewer pages.

like image 167
Andy E Avatar answered May 11 '23 00:05

Andy E