Apex Performance Tips and Tricks : Amit Chaudhary

Apex Performance Tips and Tricks
by: Amit Chaudhary
blow post content copied from  Apex Hours
click here to view original post

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, 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 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

Governor Limits Performance
Stop us from monopolising resources How to improve the way in which our code runs and uses resources
Hard limits No 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?
    • CPU Time vs Heap Size
  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

  • Integrations
  • Reporting
  • Searching

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 Exceptionis the worst exception to get

Limit Methods

  1. Salesforce provides a Limit sclass 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

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


  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

The post Apex Performance Tips and Tricks appeared first on Apex Hours.

May 17, 2021 at 06:30PM
Click here for more details...

The original post is available in Apex Hours by Amit Chaudhary
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.