Reputation: 568
I have read a few articles online about optimizing javascript loading. A few key points I got is to minimize the number of script files (http requests), minify and enable gzip on the server. Currently, what is done on my side is that all javascript files are minified, and gzip can be simply enabled as well.
Part 1)
My problem is that I have around 20 javascript files, there is one common.js that has all the core functions. Besides that, every page would load at least one other file that implements the functionality of that page.
Solution 1, is to combine all scripts in one big script file, and get loaded once for each client, which seems to be what everyone else is doing. I guess YUI or JSMin can be used for compressing, so I should combine the files manually?
Solution 2, lazy loading when a required function is needed, I don't really know how this works, but it seems to be a way to do it, though still requires more http requests. I would love to hear any inputs for this.
Part 2)
The web application I am working on will be deployed to many other computers, since javascripts are pretty small, is it a good practice to make a script that dynamically load the newest script from a central server, so that every client will run the newest script?
Upvotes: 6
Views: 7552
Reputation: 1662
You have to break optimisation into several steps
<script src='//...' async defer>
when possible, if you have script dependencies, be careful when using asycn loading. If you use AMD module, it will make sure your dependent modules are loaded in advance, and they only load once per page refresh. Upvotes: 2
Reputation: 1368
Part 1: Like you said, there's two approaches.
Part 2:
Upvotes: 3
Reputation: 5824
General best practice is considered to be
Merge into as few files as possible
Minify
Serve gzipped
Serve as late as possible (some may need to be in the head, but generally before or asynchronously is preferred
This is general advice, you need to do it and test to ensure it's right for your case.
Minimising HTTP requests is important, latency is a performance killer...
Upvotes: 3