
How To Speed Up Your Website Page Load Performance by Delaying The Load of JavaScript and Widgets - Part 2

"...So I wouldn't recommend getting rid od widgets whether they be from Facebook or whether they be ads or whatever just because the server that you are remote talking to is a little bit slower. There are plenty of ways that you can do things that are what we call asynchronous. So for example, we might look at the time that it takes until you reach the onload event, but that doesn't mean that the JavaScript can't keep happening after the onload event. So Google Analytics for example now provides a new snippet of JavaScript code that will not affect the load time in any significant way and that is also good for user experience."Now that we know that Google approves of this solution, lets go to the next stage – implementation. The problem is that none of the widgets out there – at least at this time – are made to defer the loading. They are made to run right away as the page loads. This means that we will have to make some custom adjustments to the code of the widgets.
Deferred Loading Vs. Asynchronous Loading

into a script tag.async="true"
So your script tag would start with:
However, browser support for asynchronous JavaScript value is still not fully provided. Another safer way to do it is to do something like this with the code:
On the other hand, the deferred JavaScript is there for a reason: it completely gets the JavaScript out of the way while your page is loading. For the purposes such as comment widgets, share buttons, etc., this can be a good solution because it lets the page load just as if there was no external JavaScript files being loaded. Then, while your visitor reads the page, the comments and the share button are quietly started at the bottom of the article.
What Sort of Improvement Can You Expect?

- One version was intact, as-is, with “normal” JavaScript load.
- Second was the asynchronous load.
- Third was the 10 second deferred load.
Applying the same set of solutions to all JavaScript in the page, in some cases we were able to shave off as much as seven seconds off of page load time, although on average this number was lower especially during the traffic peak times. As mentioned earlier, when the net is more congested with high number of visitors, the servers get more busy and it just takes longer time for everything to load as opposed to the time periods when there are less people on the net. Still, the results were very encouraging and the page really felt much better. You click a link, page loads very fast, as you read through the article you don't even notice that 10 seconds into your reading of the article, the Apture Bar gets activated and the comments section turns on along with the share button. This definitely improves the user experience. All we did was applying the deferred loading to five scripts and widgets and the page speed analyzers showed that we went from 131 external requests to 81 with three to seven seconds speed improvement per page. I hear you say: “Wait, 50 external requests less? From just five scripts and widgets being deferred?” Yes. When you load scripts and widgets you never get only one. They all have includes or call other scripts, and then includes frequently have more includes. Based on what we saw in our experiments, comment scripts are some of the heaviest load burdens you can have on the pages. Some of them have 20 or more external calls. For the user this means that page will take a while to load even though the content of the widgets will be shown at the bottom of the article. On the other hand, with the deferred load, you can achieve the effect that when you arrive to the page it will feel pretty much like an instant load, and while you are reading through the content, JavasScript will be working in the background preparing the additional content. Properly executed, your users will notice nothing except the pleasant experience of pages being loaded fast. As Matt mentioned in the above video, Google Analytics already uses asynchronous loading and this is a good solution that will satisfy most webmasters. Many other services don't have this feature in their widgets. Those scripts and widgets need some custom coding. Most of them require remarkably little of it, while some need a bit more work.
Widgets Speedup Examples

Apture Bar
The Apture Bar is one of the easy widgets to adjust for deferred loading. Old:
New (with 10 seconds delayed loading):
AddThis Button
Old:
![]()
New:![]()
Disqus
When it comes to Disqus comments widget the story gets a bit more complex. At the moment this article is being written, Disqus is working on a new version of the Disqus plugin for Movable Type, so in due time the new plugin will be available to load Disqus asynchronously. However, we wanted to know what can we do in the meantime and if there is something we can do that will be even better than waiting for the new plugin. Disqus (at least for the Movable Type implementation) is not a piece of code that you paste into your page, it is a CGI plugin in Movable Type which then generates the JavaScript in all pages when they are being built. We have two options to speed it up.
1) Hack the Disqus Plugin for Movable Type
In the disqus.pl file, simply delete this line:
and then just before the