Visualforce was designed to allow us to create functionality for users that has a similar user experience as the Force.com platform. Well designed pages should feel like they were meant to be part of the platform, and should be incredibly performant.
If you have developed on Salesforce any amount of time, I’m sure you have seen pages or processes that are incredibly slow. Before we can even begin to know why a visualforce page is slow, we need to take time and understand why it is. So, don’t be scared to turn on Developer mode for your Salesforce user and begin to do some profiling. Profiling will involve watch the debug logs, and understanding how long each step of the page takes. It may involve modifying the code to include additional debug lines. With that being said, let’s have a look at some best practices for improving visualforce performance.
Before doing any development or profiling, make sure to check the performance of following things
- Make sure that the slow loading isn’t confined to one computer. This could be caused by the computer being past it’s prime or even a virus scanner.
- Check that the page is performing well on a different network. Most likely any slowness related to this would be the result of latency or bandwidth.
- Check to make sure Salesforce isn’t having any issues by having a quick look at trust.salesforce.com
There’s a number of optimizations that can be made fairly quickly to improve performance. A lot of these are pretty common best practices within Salesforce and in software development.
Reduce the Number of Database Queries & Records being Used
If your page isn’t working with any data, make sure that you aren’t querying anything. If your page is working with data make sure that your SOQL is using Indexed Fields in the where clause to speed up SOQL queries, and using any additional filters to reduce the amount of data.
Take Advantage of Browser Caching
Reduce the Size of Files
Images that are purchased from websites like shutterstock or were taken with a phone or camera are often uploaded to the internet without reducing the size of the file to the needed size and aren’t usually optimized for the web. Websites like kraken.io can usually reduce the size of images by at least 30 to 50%.
Avoid SOQL Queries in get methods
Salesforce won’t necessarily cache the results of the database query, which can result in running the same query multiple times. ALthough there’s a pattern that can be used to cache the data, it’s really not a good practice as it will just increase the size of the view state.
Reduce the Size of the View State
View State is basically a lot of extra data that will be transmitted with every request. A maximum of 135kb can be transmitted per request, which doesn’t sound like a lot but it can really add up. I’ve previously blogged about why reducing the view state is important, and how to do it. Check out the How To Reduce View State post for more information.
Reduce the Use of Visualforce Components
Not only do visualforce components potentially increase the size of the view state, but they can also dramatically change how long it takes to render a page.
What are some other optimizations that you think should be done to improve page performance?
Author: Brian Cline