Current time: 12-08-2019, 10:38 AM Hello There, Guest! (LoginRegister)

Post Reply 
 
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Asynchronous Javascript
08-17-2012, 01:09 AM
Post: #1
Asynchronous Javascript
In order to reduce page load times I want to asynchronously load all the Facebook/Twitter rubbish that now seems obligatory on a webpage. I have tried to do that by putting it all in a php include placed near the bottom of every page. The code looks like this:


<!-- Facebook -->
<div id="fb-root"></div>
<script>
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=XXXXXXXXXXXXXXX";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>

<!-- Google +1 -->
<script type="text/javascript">
window.___gcfg = {lang: 'en-GB'};
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/plusone.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
</script>

<!-- Twitter -->
<script>
!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
</script>

<!-- Analytics -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXXXX']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>

Unfortunately it doesn't work. Angry

Without that code, my page loads in about 1.9 seconds. With the extra code it takes 5 seconds, and WebPageTest clearly shows that the various Google/Facebook/Twitter files are loading early in the page load process and consequently slowing the whole thing down.

Am I doing something wrong? Well, obviously I am, but what? Please be gently with me: I'm a javascript virgin. Angel
Find all posts by this user
Quote this message in a reply
08-17-2012, 11:36 PM
Post: #2
RE: Asynchronous Javascript
Any chance you have a link to a test result you're willing to share?

For starters, async javascript will block onload for all modern browsers (IE < 9 I believe being the main case where it does not) so loading the scripts async just lets the browser load them in parallel without blocking the DOM parser but depending on when you initiate the async load it could actually make load times longer (by starting to load asynchronously later). With async scripts you're actually better off kicking them off earlier and just letting them do their thing in parallel with the rest of the page loading activities.

Depending on how complicated the page is, the browser's parser might actually be getting to the end of the HTML really quickly. Any images it encounters along the way are just scheduled to be loaded and the parser continues on with the DOM. If you use Chrome or Firefox as a test agent you will see a pink vertical bar that is the DOM Content ready event which is basically the point in time where the parser finished building the DOM and made it to the end of the html.
Visit this user's website Find all posts by this user
Quote this message in a reply
08-18-2012, 12:39 AM
Post: #3
RE: Asynchronous Javascript
I did a little bit of a shuffle around and seem to have got the page load down to about 4.6 to 4.8 seconds. Here is a sample test result:
http://www.webpagetest.org/result/120817_80_D2P/

It looks like all the DOM content is complete in 3 to 3.5 seconds, which still seems pretty poor.

On the other hand, I ran my own test by clearing cache, cookies and everything out of IE9 and then loading the page. It's hard to put a figure on how fast it loaded because it was too quick to time accurately. Certainly it was entirely acceptable. It looked like a second or so. Maybe I'm being hyper-critical and should live with what I've got and move on to more important things.
Find all posts by this user
Quote this message in a reply
08-18-2012, 06:11 AM
Post: #4
RE: Asynchronous Javascript
FWIW, IE 9's dev tools has a network trace view that should give you a waterfall as well an indication of how long the page took to load.

How fast is your connection to the Internet and how far away is the server? A low-latency high-bandwidth connection could pretty easily account for the difference.

It does look like the user-visible time is largely dominated by the back-end (time to first byte) of about a second and then all of the social widgets (which they won't really care about so it should feel like it is loading quite fast).

If you turn on video capture you can see what the visual experience felt like from the perspective of the test agent.
Visit this user's website Find all posts by this user
Quote this message in a reply
08-18-2012, 07:59 AM
Post: #5
RE: Asynchronous Javascript
My connection is only 2.6 Mb (real, from a speedtest) so I think most people will have a connection at least as good.

The network trace in IE9 was quite interesting, though not quite as easy to read as WebPageTest.

For the moment, I think I've convinced myself that the page load is good enough to move on to other things, like actually getting the site completed and operational. Once it's up and running, I'll have another look to see if I can gee it up any further.

Thanks for your help, Patrick.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


User(s) browsing this thread: 1 Guest(s)