Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice to combine JS files [closed]

Tags:

javascript

I always have this questions in mind come to JS optimise, nowadays most people combine all their CSSs into single file by using Less, Sass or others methods. but come to JS i am a bit hesitate on the approach, cause there are plug-in, frameworks and your own code. Just wondering is there a rule or best practice to approach.

so should i combine all my JSs into single JS include plug-in, frameworks, libary and my own code into one or keep them modularized accordingly.

I know it may depend on the size of the project, but what's the measurement and when I should combine all into one or modularize. Is there any rules I should be followed.

Any suggestion are appreciated.

like image 896
Bill Avatar asked Aug 29 '14 00:08

Bill


People also ask

Should I combine JavaScript files?

Combining files can be better for performance # For my bigger plugins, I like to work with a handful of smaller, more modular files instead of one massive 3,000 line JavaScript file. Sometimes, doing so also saves me a lot of work and keeps my code more DRY.

How do I combine multiple JavaScript files into one?

Use a javascript bundler to combine many smaller scripts into a custom file per page. This is more complicated, but allows you to reuse code, and have a tool replicate the code for you. Dynamically require the scripts while the page is loading.

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.

Can you have two JavaScript files?

You can either have separate files or a single JS, as always there are tradeoff whatever route you take, so it's a matter of understanding the HOWs. Besides that, the issue is probably when you execute your javascript code.


1 Answers

It's generally a good idea to combine and minify your own development JavaScript. Having multiple HTTP requests can slow down load times if there are too many requests (especially if there are multiple small files). Google PageSpeed Insights gives some guidelines on how to do it here.

As @veroxii says, most people end up using a "build" since minifying and combining everything manually would be a huge waste of time. For small sites that I work on that don't really have a built in minification system, I like to use gulpjs along with gulp-uglify and gulp-concat to minify and combine javascript resources.

You have to be careful when combining though because often times, scripts will depend on other scripts. Say you have two scripts that you combined where scriptB depends on scriptA. If the browser loads and runs scriptB before scriptA because it came first in the combined file, then bad things will happen. Either be careful with your script combination or use something like requirejs.

You can't really do much in terms of minification or combination when using a third party script loaded in from a CDN (like jQuery) except use the production script.min.js resource that they provide. You could potentially download their script and throw it into your minification process, but most users are more likely to already have the CDN version cached by their browser.

The biggest thing when it comes to JavaScript is making sure the loading of the scripts don't block the rendering of the page. Most JavaScript is useless without content, so why not let the content load first and then load in the script? Users will see the content first and then interact so it's probably a good idea to load those resources in that order. More on that here. Either put your script tags at the bottom of the page, use the asyncattribute, or use an asynchronous javascript loader like loadJS or requirejs.

like image 105
tylerargo Avatar answered Sep 29 '22 20:09

tylerargo