

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

- Using the minimum amount of resources possible
- Queries
- CPU Statements/Time
- Heap Size
- Using the most efficient mechanism
- Theoretically should make us more performant
Governor Limits vs Performance
Let check about governor limit and 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
- What does the user actually experience?
- CPU Time vs Heap Size
- Perception vs reality
- UX vs Technical Improvements
- 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
- Governors ≠ Performance.
- Act a very useful guide for what we should be focusing on.
- Should measure against limits during testing.
- A Limit Exceptions the worst exception to get.

Limit Methods

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

Loop Performance
- A primary source of CPU time consumption.
- Nested loops can quickly consume resources.
- Ensure you follow best practices
- No DML in loops.
- No SOQL in loops.
- Avoid expensive operations in loops.
- Different loop options have impacts.

Heap Size Results

SOQL Performance
How to Improve SOQL Performance.

- Run fewer queries.
- Cache results in memory to reuse them wherever possible.
- 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

Asynchronicity and Architecture

- Does this need to be done now?
- Do all updates need to happen now?
- What else hangs off this event occurring?
- Options for asynchronous processing
- Future
- Scheduled
- Batch
- Queueable
- Web services
- Platform Events
Apex Performance Tips and Tricks Video
- What do we mean by performance?
- How do we measure performance?
- Loop Performance.
- SOQL Performance.
- Caching.
- Asynchronicity and Architecture.
Related Post
Summary
I hope this session will help you to understand the Apex Performance tips and Tricks. Follow Apex best practices to improve your apex performance.