Best Practices for Improving Visualforce Performance

Best Practices for Improving Visualforce Performance
Sharing is Caring

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
By storing as much of the JavaScript and CSS in static resources, we can take advantage of the browser’s caching mechanism and decrease the number of resources that need to be downloaded to make the page work correctly. Salesforce’s static resources have a lot of advantages that uploading to your company’s website may not included, caching and a cdn are already built into them.

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%.

CSS and JavaScript should be minimized before being added to static resources. Websites like minifer.org can do this if you copy and paste the file or there’s a whole bunch of build tools that can do it using JavaScript. Make sure that you keep an unminified version in your source control.

If possible, usually it is for most files, move any JavaScript include scripts to just before the body tag. Loading JavaScript files causes the browser to block other requests until it has downloaded the JavaScript files and parsed them. While doing this make sure to remove anything, you’re not actually using.

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.

Start Switching Out Apex for JavaScript
Although this is really dramatic, there is a point when pages may actually start to load faster and perform better if they are using JavaScript. To do this, developers need to really start understanding how JavaScript works and the different techniques for calling the Salesforce API or calling apex methods. This is just one of the reasons I believe that Salesforce Developers need to learn JavaScript.

I haven’t personally done it yet, but there’s a lot of different blog articles out there about using Angular or other JavaScript single page application frameworks within Salesforce.

What are some other optimizations that you think should be done to improve page performance?

Author: Brian Cline

Brian is a programmer living in Niagara Falls with ten years of development experience. He is passionate about automation, business process re-engineering, and gamification. For the last six years or so, he has been working with Salesforce and JavaScript.

Related Posts

Sharing is Caring