Is there a way to load my JavaScript into a page that will make it load faster?
|
There are a few things you can do:
Yahoo has a great page of suggestions that can help reduce page load times. |
|||||||||
|
|
Besides Minifing, gziping and CDNing (new word?). You should consider defer loading. Basically what this does is add the scripts dynamically and prevent blocking, allowing parallel downloads. There are many ways of doing it, this is the one I prefer
Place this just before the closing body tag and use AttachScript to load every js file. |
|||||||
|
|
You might want to look at the way Google loads Analytics as well:
As it is considered a "best practice" sort of script: |
|||
|
|
|
A couple of Google folks announced an new open source project at Velocity 2010 called Diffable. Diffable performs some magic to incrementally publish only the portions of JS, HTML, and CSS which have changed since the version which is stored in the user's cache, rather than sending an entire new file when a new version is released. This technique is insanely cool, and is currently most effective (and worth the effort) on websites where you are using a large JavaScript code base with small frequent code changes. This applies especially well to applications like Google Maps, which undergoes at least one release every Tuesday, and averages about 100 new releases a year. It also makes a lot of sense in general once HTML5 local storage becomes more widespread. BTW, if you haven't seen Google's Michael Jones talk about change (in a geospatial context) it's worth watching his entire keynote at GeoWeb 2009. |
|||
|
|
|
To give an update to this question. I think that in modern, the way of non-blocking loading is not needed anymore, the browser will do it for you. I have added a question to StackOverflow, I will add the content here aswel. The only difference is that the load event will be fired a little earlier, but the loading of the files themselves remain the same. I also want to add that even if the onload even fires earlier with the non-blocking script, this does not mean the JS files are fired earlier. In my case the normal setup came out best Now first the scripts, they look like this:
I have run 3 test, here are the results: Normal setup
This is just the normal setup, we have 4 css files in the head, and 3 css files at the bottom of the page. Now I do not see anything blocking. What I see it that everything is loading at the same time. Non-blocking JS
Now to take this a little further, I have made ONLY the js files non-blocking. This with the script above. I suddenly see that my css files are blocking up the load. This is strange, because it is not blocking anything in the first example. Why is css suddenly blocking the load ? Everything non-blocking
Finally I did a test where all the external files are loaded in a non-blocking way. Now I do not see any difference with our first method. They just both look the same. ConclusionMy conclusion is that the files are already loaded in a non-blocking way, I do not see a need to add special script. Or am I missing something here? More:
|
|||
|


