Apex Performance Tips and Tricks

It’s important once you have you application up and running that you also ensure it is performant – but what does performant mean? Fast? Scalable? Within Governor Limits? How about all of these and then some! In this session we will go through what is meant by performance and Apex Performance Tips and Tricks.

Also learn about how to measure the performance of your code and understand where time is being used within a transaction as well as the actions to take and some best practices to keep things running nicely.

What do we mean by Apex performance?

Resource usage/code performance

  1. Using the minimum amount of resources possible
    • Queries
    • CPU Statements/Time
    • Heap Size
  2. Using the most efficient mechanism
  3. Theoretically should make us more performant

Governor Limits vs Performance

Let check about governor limit and performance.

Governor LimitsPerformance
Stop us from monopolising resourcesHow to improve the way in which our code runs and uses resources
Hard limitsNo limit but rather an optimization goal
Avoidance rather than a target•55 SOQL Queries vs 90 SOQL Queries

User Perceived Performance

  1. What does the user actually experience?
  2. Perception vs reality
    • UX vs Technical Improvements
  3. This is what the user will care about but may not align with Governor Limits etc.

Scaling Performance

Handling a greater volume of data and users

How do we measure performance?

  • Observe
  • Orient
  • Decide
  • Act

Measuring Performance

Using Governor Limits

  1. Governors ≠ Performance.
  2. Act a very useful guide for what we should be focusing on.
  3. Should measure against limits during testing.
  4. A Limit Exceptions the worst exception to get.

Limit Methods

  1. Salesforce provides a Limits class to allow us to take measurements on how resources are being used.
  2. Measure both usage and against total available limit.
  3. Schrodinger’s Cat issues -taking a measurement can impact the measurement.

Order Analysis

Loop Performance

  1. A primary source of CPU time consumption.
  2. Nested loops can quickly consume resources.
  3. Ensure you follow best practices
    • No DML in loops.
    • No SOQL in loops.
    • Avoid expensive operations in loops.
  4. Different loop options have impacts.

Heap Size Results

SOQL Performance

How to Improve SOQL Performance.

  1. Run fewer queries.
  2. Cache results in memory to reuse them wherever possible.
  3. Ensure that your queries are selective.
    • Indexed filters
    • Implement strict sharing
    • Limits on queries

Learn about SOQL Best Practices In Salesforce.

Caching and the Platform Cache

Platform Cache

  1. What is the Platform Cache?
    • A temporary data store for frequently accessed but seldom written data.
    • Org wide and session.
  2. When should we use it?
    • Read often, write infrequently.
    • Async jumps.
    • Exchange rates as a prime example.

Asynchronicity and Architecture

Synchronous vs Asynchronous

  1. Does this need to be done now?
  2. Do all updates need to happen now?
  3. What else hangs off this event occurring?
  4. Options for asynchronous processing
    • Future
    • Scheduled
    • Batch
    • Queueable
    • Web services
    • Platform Events

Apex Performance Tips and Tricks Video

  1. What do we mean by performance?
  2. How do we measure performance?
  3. Loop Performance.
  4. SOQL Performance.
  5. Caching.
  6. Asynchronicity and Architecture.
YouTube video

Related Post


I hope this session will help you to understand the Apex Performance tips and Tricks. Follow Apex best practices to improve your apex performance.

Amit Chaudhary
Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates.

He is a active blogger and founder of Apex Hours.

Articles: 466

Leave a Reply

Your email address will not be published. Required fields are marked *