Just want to ask the gurus out there about this. I know that CSS files are better merged instead of separated into a zillion files. Does js work the same way? Here's a few points I currently know(or believe...so you can point something out if my belief/understanding is wrong):
You can write your JS in separate files, but when it comes to deploying, it's more efficient to minify them all into a single file. For each script you load in your browser, you make a round-trip to the server, so it makes sense to minimize those.
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.
It is best to keep separate files or include all files in one file Javascript? To avoid multiple server requests, it is better to group all your JavaScript files into only one.
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.
You don't need to use the same files for development and production, so you can have the best of both worlds: modularisation for development, and concatenation for deployment. Here's a list of tools for concatenation and minification.
It's a trade-off between the increased overhead of separate files and the chance you're having users download code they'll never use. You should look at how users use your app.
Consider using a build script to concatenate your files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With