You are using an older browser that might negatively affect how this site is displayed. Please update to a modern browser to have a better experience. Sorry for the inconvenience!

How to improve the performance of Visualforce Page


By: Venkata

Use Standard Functionality 

Whenever possible, try to use the declarative or standard feature such as Approval Processes, Visual Flows, and Workflows because these components are already fully optimized by the Force.com; therefore, it won’t count against most of the governor limits but in some cases business may need complex logic; then we can go for the programmatic features. Always follow the best practices for Apex and SOQL, so that governor limit won’t get hit.

Follow best practices for Apex and SOQL 

Remember the following key points, while writing Apex or SOQL for use with a Visualforce page.

  1. If possible, do calculations in SOQL instead of doing it in Apex (e.g.: Aggregate query).
  2. Don’t perform DML operation inside loop.
  3. Always perform Filter in SOQL first, then in Apex, and finally in Visualforce.

For example, don’t do the following.

<apex:pageBlockTable value="{!positions}" var="position"> 

<apex:column value="{!position.name}" rendered="{!IF(position.Status__c == 'Open - Approved', true, false)}"/> 

<apex:column value="{!position.Job_Description__c}"  rendered="{!IF(position.Status__c == 'Open - Approved', true, false)}"/> 

</apex:pageBlockTable>

Controlling Data Size 

The maximum response size for a Visualforce page shouldn’t exceed 15 MB, and we can use the following techniques to minimize the response size or amount of data each page display.

  1. Filters

Always try to use filters in SOQL calls to reduce the data size. For example – use ‘AND’, ‘OR’ statement in the WHERE clause or filter out the null result records

  1. Sharing Keyword

Use Sharing keyword in your apex controller to retrieve only the records that user can access, and this will reduce amount of unwanted data.

  1. Use StandardSetController Built-In Pagination

Always use the pre-built Visualforce list controller called ‘StandardSetController’, it prevents list view from displaying unbounded data. It allows the developer to configure list view to display up to 100 records at a time, but by default it returns 20 records on the page.

Writing Efficient Getter Methods 

Visualforce pages may sometimes need to make multiple calls to Apex classes and this may result in calling the getter methods multiple times; so we should configure the getters in your apex classes in such a way that the records will be queried only if the object instance is null.

For Example: Don’t write a statement like this:

public List getRelatedOpprtunities() { 

           return [Select Name, Amount, StageName From Opportunity Where StageName = ‘Closed Won’]; 

    } 

} 

//Re-write above method – this method fetches the opportunity record only when the instance is NULL

public List getRelatedOpprtunities { 

   get { 

        if (relatedOpprtunities == null) { 

           relatedOpprtunities = [Select Name, Amount, StageName From Opportunity Where StageName = ‘Closed Won’]; 

        } 

      return relatedOpprtunities; 

      } 

  set; 

} 

Lazy Loading 

Lazy Loading is a technique in which a Visualforce page will load the required features first and delay the rest. So, this way the user will have faster access to required features, and improved responsiveness of a large page.

To lazy load parts of a Visualforce page:

  1. Try to use the rerender attribute on Visualforce components to update the component without updating the entire page.
  2. Try to call functions of your controller using JavaScript Remoting through JavaScript.
  3. Create a custom component to show and hide data according to user actions.

Optimizing the View State 

Any Visualforce pages that is written within a form component will have the View State. The View State is nothing but a holding place of the state of the page and it includes the components, field values, and controller state etc. The performance of the Visualforce page depends upon the size of the view state. So here are some best practices that can help optimize the view state size.

  1. Always try to use the transient variables in Apex controller since they aren’t essential for maintaining state and aren’t required for page refreshes.
  2. Large percentage of view state is consumed by the query used in your apex controller; so, always write the SOQL query with required filter to reduce the view state.
  3. Use filters and pagination, if you are trying to display large set of data on Visualforce page; so that the data that increases state view size can be reduced.
  4. Instead of using multiple forms on a same page, try to use <Apex:actionRegion>. ActionRegion is nothing but area of Visualforce page that needs to be processed by the Force.com server, when an AJAX request is generated.