I’ve been doing Facebook development for a while. In the beginning I found myself searching around for answers, this post is aimed at people like me who are searching for some helpful tips on Facebook Application Development. This post deals with some technical and non-technical aspects of Facebook Development that I came across and is by no means exhaustive.
If you’re looking for speed, then you’re going to be very disappointed, the Facebook API certainly isn’t the fastest thing on the planet – it doesn’t take many API calls to get the page to load in 4+ seconds, added to that any additional overhead and you can quite easily hit the 8 – 9 second time-out limit. To try and overcome this I have a few tips that have helped me speed up the process a little:
1. Less is more!
The old saying is true, sometimes the less you do something, the better it is. The same goes for the Facebook API – the more you make API calls the slower the page will be.
Some of the common causes that I have found is calling the API inside a loop, not only does this have the potential to create hundreds of requests, but it also will cause a noticeable performance hit, so looping through a user’s 350 friends to get their names isn’t the best way to get things done.
If at all possible try and limit the number of API calls per page, there are usually FBML tags that do things a lot faster than calling methods on the API.
2. Batch API Calls
For situations where many API calls are unavoidable, try using the Batch API. By doing this you will save quite a lot of overhead by requesting all the data in one large chunk, and not in multiple little pieces that take longer because of the time taken to transfer the data. Depending on the usage you will see a relatively big performance increase by using this method.
$facebook->api_client->begin_batch(); $friends = & $api_client->friends_get(); $notifications = & $api_client->notifications_get(); $facebook->api_client->end_batch();
While you aren’t allowed to store a user’s data forever you are allowed to store it for up to 24 hours. In addition to this there is some data that you are allowed to store, for a full list take a look at the Storable Data documentation. One of the biggest things is to decide if any of the allowed storable entities would save you an API call or two, and then see if replicating some of the data in your own databases would lead to better performance.
4. Quick Transitions
For me this was perhaps the most noticeable performance boost of them all, and it was only be chance that I discovered it. To see for yourself:
- Go into the Developer App
- Go to your application settings
- Click on the canvas tab
- At the bottom there is an option called “Quick Transitions” – turn it on.
5. General Optimisations
I maybe stating the obvious but it’s worth saying just in case.
All the optimisations above will help speed up your application, but the most important thing to do is to make sure ALL your code is as optimised as it can be, not only PHP, but also the markup and CSS, theres no point in making the API calls as efficient as possible if the app is just going to sit there for a few seconds while it decides what to do with it.
There’s not much I can really say about how and what to optimise, but I did find the following code useful to check the page execution times:
$start = microtime(true); // Do stuff in here $end = microtime(true); $time = $end - $start; echo "Done in $time seconds";
Use this code to test the execution time of the page or a certain function or method call.
Happy Performance Tuning!