Is it better to merge all JavaScript files into one, or to keep them separate?
-
I am mostly concerned about load times. Convenience isn't an issue here. Because browsers can load up to 6 javascript files at once, does the added http request even make a difference, since it would appear to the user as just one? Also, under that assumption, the smaller file sizes would mean that the user would be able to load the javascript files faster.
-
Answer:
Donald Knuth famously wrote in 1974: Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%. Source: http://sbel.wisc.edu/Courses/ME964/Literature/knuthProgramming1974.pdf (emphasis added) I think minimizing HTTP requests is a very important area of web performance, so don't get me wrong. I quote Knuth because his statement is extremely valuable to anyone seeking to optimize a system; make sure your optimizations are meaningful. The best way to measure the impact of an optimization is to run tests. Try setting up an A/B test; load 6 files individually and load one concatenated file. Run this test multiple times, in multiple browsers, in empty/primed cache environments. Study your users' experience with your site. How many visits are with an empty cache vs a primed cache? These findings will tell you whether your optimization was meaningful or not. How much faster does the page actually load? If it was marginal, maybe there is a more critical area (the 3%) that needs the attention.
Rick Viscomi at Quora Visit the source
Other answers
Since you ask specifically about serialization, I would say, it depends. HTTP 1 requests are expensive - if your site is due to launch soon, I would say yes, serialization should be part of your asset delivery strategy. In the very near future, however, HTTP 2 requests (to the same server) will become very, very cheap. So cheap, in fact, that serializing scripts is about to be reduced from best practice to an anti-pattern. The short explanation is that HTTP 2 is very good at delivering many small assets - there is almost no bandwidth overhead, and with proper server setup, negligible server overhead, compared with serialized assets. The issue, then, is caching - if you serialize 20 assets of 10K each, every update to any of those assets require a new 200K delivery to each client; whereas, without serialization, returning clients need only download the individual 10K assets that expired since their last visit. For that matter, with exclusive SSL delivery on HTTP 1 with SPDY, it's hard to argue in favor of serialization - it doesn't make a substantial difference. You didn't ask about minification, but my general attitude is, don't. If the server is properly configured to deliver assets using gzip compression, the difference between a gz/minified and gz/non-minified asset is negligible - we're talking a few percent difference, for static assets that are going to get cached by the client either way. Instead of investing your time in a complex asset delivery pipeline, invest your time in building better software - ignore the serialization (and minification) issue until, or if, your site becomes a mass audience success, and at that point, maybe some optimization could be worthwhile, but, in my opinion, save those optimizations for if/when they become necessary.
Rasmus Schultz
We use multiple js files pulled into one large one. But we also make sure to have gzip set on the server to compress on the fly to browsers. I would recommend that if you use the many into one you make sure you can tell what is in what individual files for when you do debugging
Greg Johnson
It really depends to me. For example do you do daily update on your js files or do you have a "static" website? If you do daily updates in some of your scripts maybe is better to have vendors libraries together and the main application files separated.
Giovanni Bastianelli
JS and CSS should definitely be minied, as this will reduce size and make HTTP delivery quicker.YUI Compressor should be the quick and safe choice for junior - medium Javascript programmers. It's the free library tool, can be integrate in many programming and platform.If you are not a programmer, you can try this free online service using YUI Compressor, it can http://www.online-code.net/minify-js.html and http://www.online-code.net/minify-css.html free online. It's easy to use, don't need you download or install any software, only need your copy your js and css code to the webpage, then you can get the minified code back. It's very easy and simple.If you want to edit your javascript and css code, you need first unminify the js and css file to a human readable format, and then modify the js and css code.
Mike Li
As pointed out by others, pre-optimization is bad. If it's not a pain point, then I wouldn't worry about it unless you have nothing else more pressing to worry about. If you have a pain point, it's probably mobile. Even the fastest mobile networks have relatively high latency. Stacked latency will make page-loads take forever. It's been a while since I did any research on the topic, but if I remember correctly, you want to minify and inline any code that is required for display of above the fold content into the first 16kb of the page. After that, you start stacking latency. However, that's optimization for a fast loading content or landing page. If you're working on a single page app or something similar, especially if it's one that is undergoing regular changes, you'd be much better off keeping the files separate, organized in whatever way makes the most sense to the developer. Focus on making sure things get cached properly and consider using a CDN.
Nick Husby
@pete hunt has a simple and concise explanation in his talk about how http://Instagram.com works:
John Babak
My answer is: merge your own code, but not widely used libraries like jQuery. Reason is that those libraries are probably faster served through CDN they are already on. If time and budget allows and situation requires, test different options for different visitor sources.
Sergey Gerbek
Will reducing the number of HTTP requests improve load performance? Yes. Will reducing the file size of your files improve load performance? Yes. Using tooling such a http://Grunt or http://Gulp you can set up minification tasks in minutes. Just do it, this will free you up to benchmark performance optimizations that are actually worth focusing on.
Ryan Nel
IME, under almost all circumstances, it doesn't make a lot of difference. As others have stated, this seems a lot like premature optimization. For some reason, we coders are extremely vulnerable to that kind of mistake. Also from my experience, bottlenecks and performance black holes in web projects are to be found, in descending order of importance: In database queries In the server-side application code In server-side networking (e.g., the application needs to fetch data from another server) In client-side javascript programming (lots to be said here, e.g. "getter" functions can cost a lot) In massive page reflows caused by complicated css and js gui code Even if you have it down to the JS, there's still a question whether it is actually the LOADING, and not the parsing or the code itself that causes the problem. In any real-life scenario, what I would do is: Do whatever seems more convenient. If trouble arises and there is reasonable suspicion that the JS is to blame, give the other solution a try. Keep testing whether the organisation of the JS files really is the issue. (I'm not quite sure that the same goes for CSS files, though. A single huge CSS file for a whole site that is loaded/parsed and applied to every page even though 90% of the code isn't even necessary for that page, actually seems to cause trouble.)
Christian Friedl
Related Q & A:
- How do I loop through many files in one folder?Best solution by thespreadsheetguru.com
- How to merge multiple CSV files into a single CSV file?Best solution by solveyourtech.com
- How to separate mongodb data files from the journal?Best solution by dba.stackexchange.com
- How to move all music files into one folder?Best solution by stackoverflow.com
- What are .cue files and how to separate a large flac file?Best solution by techsupportalert.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.