The current ad code for Where is Croydon looks like this:
<script type="text/javascript"><!--
google_ad_client = "pub-7600935420912685";
google_ad_slot = "7690333966";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
And performs like this:
Document Complete | Fully Loaded | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Load Time | First Byte | Start Render | DOM Elements | Time | Requests | Bytes In | Time | Requests | Bytes In | |
First View | 1.939s | 0.289s | 0.426s | 187 | 1.939s | 12 | 52 KB | 2.438s | 14 | 53 KB |
Repeat View | 1.054s | 0.228s | 0.574s | 182 | 1.054s | 2 | 6 KB | 1.054s | 2 | 6 KB |
Document complete in about 2 seconds is not good enough, (given our page has nothing on it), and the advert script is going to slow down any other asset downloads (by putting the browser into serial mode).
So how can we fix this? a bit of re-plumbing should fix this, our ad script becomes:
<script type="text/javascript"><!--
google_ad_client = "pub-7600935420912685";
google_ad_slot = "7690333966";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"><!--
// dynamically Load Ads out-of-band
setTimeout((function ()
{
// placeholder for ads
var eleAds = document.createElement("ads");
// dynamic script element
var eleScript = document.createElement("script");
// remember the implementation of document.write function
w = document.write;
// override and replace with our version
document.write = (function(params)
{
// replace our placeholder with real ads
eleAds.innerHTML = params;
// put the old implementation back in place
document.write=w;
});
// setup the ads script element
eleScript.setAttribute("type", "text/javascript");
eleScript.setAttribute("src", "http://pagead2.googlesyndication.com/pagead/show_ads.js");
// add the two elements, causing the ads script to run
document.body.appendChild(eleAds);
document.body.appendChild(eleScript);
}), 1);
//-->
</script>
How does this work? Google's Ad script is quite "nasty" as it uses document.write, generally not a good idea as it can only be used during page creation. So this script does two things:
- it dynamically inserts the Google ad script, allowing this to be separate from the main page render, hence the rest of the page can load without waiting for the adverts
- it re-plumbs the document.write function to instead add the ads to a placeholder element. If this is not done then the Google code will try to call document.write and it won't output anything as the document is already complete. For completeness the code re-plumbs document.write after the first call, but really there should be no other code calling it.
What does all this effort yield us:
Document Complete | Fully Loaded | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
Load Time | First Byte | Start Render | DOM Elements | Time | Requests | Bytes In | Time | Requests | Bytes In | |
First View | 0.484s | 0.332s | 0.584s | 191 | 0.484s | 1 | 3 KB | 2.353s | 13 | 55 KB |
Repeat View | 0.347s | 0.211s | 0.611s | 186 | 0.347s | 1 | 0 KB | 1.185s | 2 | 6 KB |
Fully loaded has not changed (as expected, we are still loading the same amount), but document complete is massively improved - from 2 seconds to less than half a second. Whilst this might seem to be moving numbers around, it means that the browser can render the rest of the page before adverts, and since adverts can be very very slow, this can make the site appear much faster.
And for those of you who do want to cheat performance numbers, then changing the "}), 1);" line to "}), 4000);" gives you this. A page that is "fully loaded" in 0.39 seconds - this is of course cheating, the adverts appear 4 seconds later, but https://sites.google.com/a/webpagetest.org/docs/using-webpagetest/quick-start-quide#TOC-Fully-Loaded: looks for 2 seconds of no activity to judged fully loaded time.
1 comment:
Hi there
I guess this change is not allowed by the google rules. :-(
I really have a problem, my ads are loading extreme slow. Means the page load time is about 3 seconds now
1,4 seconds for the page, which is okay
1.6 seconds for adsense - makes me nervous.
I recently heard that google has changed their adsense code to asyncronous, but I did not experience any change:
http://www.webmaster-source.com/2011/03/18/google-rolling-out-asynchronous-adsense-ads/
I am really thinking of using your code.
Post a Comment