Google +1 Button Performance Review
aaronpeters.nl
aaronpeters.nl
<script>
(function(d, t, g, s){
g = d.createElement(t),
s = d.getElementsByTagName(t)[0];
g.async = true;
g.src = 'https://apis.google.com/js/plusone.js';
s.parentNode.insertBefore(g, s);
})(document, 'script');
</script>
Load it as far up your page as possible, as to benefit from the parallelism early on and not hanging it off the end of or extending your page load.- Define g and s as parameter even though they're only given a value inside the function
- Pass document into the function instead of using it from global scope
- Insert a script tag in front of the other script tags on-the-fly using insertBefore, instead of simply doing <script async src="...">...</script>
Pretty interesting. Care to explain a bit?
- Define g and s as parameter even though they're only given a value inside the function
- Pass document into the function instead of using it from global scope
This is not necessarily true: javascript passed arguments by reference, and thus g and s are actually "given back" to the caller, who can then do different things with those objects.
d is not necessarily a document, but can be something different too: consider, for example, the contentWindow of an iframe.
Pass by reference doesn't work that way in JS;
function a(b) { b = 10; }
var foo;
a(foo);
alert(foo);
Won't display "10". It behaves like passing pointers, not mutable references such as the C++'s &.Then the only thing I can think about is that accepting it as an additional parameter in the function call is less overhead (as in, bytes) than defining 'b' as a var:
function a(b)
{
b = 10;
}
is smaller than function a()
{
var b = 10;
}
and doesn't pollute the global namespace.EDIT: I see dave1010uk also said this.
Creating a new script tag with JS lets you get asynchronous script loading for older browsers that don't support the async attribute.
Some other ways to reduce bytes are outlined at https://github.com/jed/140bytes/wiki/Byte-saving-techniques
"As for your question, we pass in `document` that way to prevent unnecessary scope lookups. We could’ve used `var d = document` inside the IIFE, but that would trigger an additional scope lookup. For `'script'` it doesn’t matter; you could initialize that inside of the IIFE if you want."
Perhaps if this was inside a tight inner loop, I could understand, but here it's completely unneccessary.
Then again, Javascript is a strange animal here as the code is provided to the execution environment as plaintext instead of bytecode/assembly. I guess a JS-to-JS compiler such as closure could handle it.
g.appendChild(d.createTextNode('{"lang":"fr"}'));Google is evangelising performance on one hand and making careless errors on the other...
(</sarcasm>)
There's another performance review of +1 here [3], which compares it to the Facebook Like button. Steve Souders (who's written books on web performance) has detailed and compared a few other 3rd party JavaScript buttons and widgets here [4].
[1] http://adage.com/article/digital/google-adds-button-foray-so...
[2] http://googlewebmastercentral.blogspot.com/2010/04/using-sit...
[3] http://www.artzstudio.com/2011/06/googles-button-is-slow-and...
- It may not run javascript at all.
- It doesn't need to be checked for malicious content.
- If all else fails, it might have it cached already.
Even if external assets are computed into a site's speed, the overhead is negligible when it comes to search ranking.
As the article says, even with a primed cache, the +1 code slows down page load. It also said the overhead can be 2 seconds. If you're aiming for fast then that could double your page load time. External assets are very likely to be calculated into a sites speed as they generally take up quite a bit of the page load time.
Of course, no one knows how much of an effect page speed plays in Google's algorithms. My bet, however, is that adding +1 to a fast site could easily make it drop a few places in the SERPs. If your site is already quite slow then an extra 2 seconds may only be a few % slower.
I mean, come on. Sites with maps, ads, or even big images can take up more than two seconds. It's not a big deal. Your analytics tracking code slows everything even more.
And it's the same network, the crawler might just go to the next server room.
Edit: you can. This example [1] has opacity:0.3 so you can still see it. It seems as though it would be trivial for an attacker to put the +1 button over every link on a page. You may even be able to have one tracking the mouse cursor.