63
votes
Accepted
Why is Schema.describeSObjects(types) Slower Than Schema.getGlobalDescribe()?
I suspect, internally, that this code is written in Apex Code and thus suffers the same general performance problems as doing it yourself. If you want a blazing-fast interface, consider using Type ...
43
votes
Accepted
Why Would I Not Enable Chaining?
This is mostly a matter of preference. Using chaining tends to create more readable code, and can actually reduce CPU/heap memory. For example, consider these two snippets of code:
// No chaining
...
33
votes
Accepted
When To Use Set vs. List In SOQL
(tl;dr at bottom)
Filter Speed
If you can use Map.keySet(), you'll get a Set back in about 1/10th of the time versus a loop over those same records and adding all the values. Of course, the Map<...
27
votes
Accepted
Performance: List.get(index) vs List[index]
The following methodology may be somewhat faulty but demonstrates a large difference. The get approach is more than 4 times slower.
Square Brackets
List<Integer> numbers = new List<Integer&...
26
votes
Why Would I Not Enable Chaining?
I'll play devil's advocate with some reasons you might not want to chain methods. (I'm not necessarily saying a fluent interface is a bad thing, just providing some counter points.)
Debugging and ...
25
votes
Accepted
What is the difference between (Id)str and Id.valueOf(str)?
The first is casting. The second is calling a static method on the Id class.
One major advantage of the former is that it is more null safe.
Id idVal1 = (Id)null; // works
Id idVal2 = Id.valueOf(...
24
votes
Accepted
How to avoid instantiating object inside a loop?
Recommendation
Object instantiation is fairly cheap. However, you can make it more efficient in two ways:
Set field values using name/value pairs.
Don't cache the object, just add it directly to the ...
24
votes
Dramatic increase in AsyncApexJob latency
When you say "schedule a task", I'm going to assume you're talking about a batch class that performs work. If that's the case, I believe your issue is related to how asynchronous processing works on ...
21
votes
Accepted
Visualforce PageReference - Name Constructor vs. Factory
Today I Learned. From the Spring '17 Release Notes on Allow CSRF Protection on GET Requests to Visualforce Pages (Critical Update):
CRSF [sic] checks on GET requests also affect how Visualforce pages ...
18
votes
Is it more efficient to use a map and call .get(), or use a set and call .contains()?
In pretty much every concievable scenario, the number of records (or data in general) that we can work with in Salesforce in a single transaction means that any difference between approaches is likely ...
17
votes
Accepted
How to implement on scroll action in lightning to show next few list of records
You could implement infinite scrolling similar to what I did for my custom Lightning Component that renders data table.
In your main component's render.js file attach a listener to the window....
17
votes
Accepted
Code Optimization
There are a number of optimizations that could occur here, but none of them are "major." Here goes.
Stop Describing!
This one may or may not be your main culprit, because you're not caching your ...
17
votes
Accepted
Which is faster between static and dynamic getSObjects?
Functionally, there is no difference. These two options return the same data. These functions are mostly the same.
From a performance perspective, the static approach is approximately 2.5x faster. I ...
16
votes
Accepted
Obtaining Single Record - SOQL vs. Instantiating New Object
Do not use SOQL unless you must. If all the data you need to update comes from the child, simply use it sans query. Save governor limits wherever possible in your code.
Opportunity master = new ...
16
votes
Accepted
Performance Impact of DescribeSObjects vs. Individual Describe Calls?
Getting the individual describes is significantly faster. Depending on your situation, it is anywhere from 14 to 850 times as fast!
Describing Many Objects Once
The first continuum I profiled is the ...
16
votes
Accepted
What's In A Hash Collision?
The difference is how many times equals will be called (and thus, your performance). As an example, let's take a look at the following class:
public class KeyTrial {
public static Integer ...
14
votes
When To Use Set vs. List In SOQL
Update: I missed what is probably the single most important part of the question in answering this - the "In SOQL" part. Perhaps consider this answer in terms of the other processing you are going to ...
13
votes
What is the difference between (Id)str and Id.valueOf(str)?
Casting is not required to convert a String to an Id:
String strVal = '001000000000000AAA';
Id idVal1 = strVal;
Id idVal2 = '001000000000000AAA';
Id is a specialization of String.
This can be shown ...
13
votes
What is the performance penalty for Deterministic Platform Encryption?
I encrypted a few PII fields on Contact:
and ran this script to insert and then query back 200 Contacts:
delete [select Id from Contact where createddate = TODAY];
/* test cost of encryption */
...
13
votes
Why are foreach loops slower in Apex than standard for loop?
It's because for each loops end up using an Iterator, which is inherently slower than a normal for loop. Here's what it might compile to internally:
Iterator<Object> iter = source.iterator();
...
13
votes
Accepted
Infinite queueable chaining, what is the drawback?
The primary limitation you will face is throttling. Once you surpass a chain depth of 4, Salesforce will slow you down to 15 seconds between executions. If you need to process a high volume of jobs, ...
13
votes
Is it more efficient to use a map and call .get(), or use a set and call .contains()?
I'd come at this from a different direction. I don't think the performance aspect is that interesting - it essentially falls into Premature Optimization and I presume the difference is negligible ...
12
votes
Accepted
Syntactic vs Performance difference on object creation?
The second form is approximately twice as fast. I wrote a quick test to prove this. 10,000x records with two fields takes about 250ms using the SObject constructor, and 500ms by assigning actual ...
12
votes
Anyone else seeing unexpected "System.LimitException: Apex CPU time limit exceeded" in previously working code?
I know this is not an answer but I don't have enough rep to comment
We have experienced this in 2 of our CI ORGs which are mapped to 2 different code bases (one of them hasn't changed in about a ...
12
votes
Accepted
Most optimal way to Enforce CRUD/FLS in Apex
You could write a helper method for this type of functionality:
public Object flsGet(SObject record, SObjectField field)
{
return !field.getDescribe().isAccessible() ? null : record.get(field);
}
...
12
votes
Accepted
RemoteAction Vs REST? Which is faster?
@RemoteAction is the fastest you can go without using API calls. If you don't mind using API calls, the AJAX Toolkit is very slightly faster. That said, a well-written RemoteAction can easily run in 1/...
12
votes
How to avoid instantiating object inside a loop?
As Adrian Larson pointed out, object instantiation is pretty cheap.
One pattern that I've used in some places is to create a base instance outside of a loop, setting as many common fields as possible, ...
12
votes
Accepted
SOQL Performance With LIMIT 1
In the case of assignment to a single record and filtering on an Id, there is zero impact on your governor usage. It won't affect CPU Time, and if you were already enforcing exactly one result by ...
12
votes
Visualforce PageReference - Name Constructor vs. Factory
One important aspect, besides the CRSF limitation, is that when you use the string constructor, you're explicitly avoiding creating a metadata link between your page and the class.
What this means ...
12
votes
Should trigger handlers be static or non-static?
We use the object oriented approach of using non-static methods because of the massive benefits we can derive in unit testing (and potentially also in production cases, though we don't do that yet).
...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
performance × 346apex × 132
soql × 54
trigger × 30
visualforce × 25
bestpractice × 25
lightning-aura-components × 24
cpulimit × 19
lightning × 18
query × 17
batch × 16
governorlimits × 16
javascript × 12
marketing-cloud × 9
lightning-web-components × 9
api × 9
deployment × 8
rest-api × 7
dml × 7
loop × 7
unit-test × 6
aura × 6
data × 6
limits × 6
community × 5