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?
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.
The Optimize JavaScript API allows you to use your own JavaScript code to make changes to variants using callback functions. You can also choose to combine these code changes with edits made using the Optimize visual editor.
Part 1: Like you said, there's two approaches.
Part 2:
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...
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. 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