Skip to main content
65 votes
Accepted

Why is Schema.describeSObjects(types) Slower Than Schema.getGlobalDescribe()?

Edit: In later API versions, Type.forName(...).newInstance() appears to now suffer a significant time penalty. In addition, Schema.describeSObjects performs very well when describing objects in bulk, ...
sfdcfox's user avatar
  • 497k
25 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 ...
Adrian Larson's user avatar
  • 151k
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(...
Adrian Larson's user avatar
  • 151k
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 ...
crmprogdev's user avatar
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 ...
Adrian Larson's user avatar
  • 151k
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 ...
Derek F's user avatar
  • 62.7k
17 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 ...
Adrian Larson's user avatar
  • 151k
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....
Doug Ayers's user avatar
  • 4,196
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 ...
sfdcfox's user avatar
  • 497k
17 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 ...
sfdcfox's user avatar
  • 497k
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 ...
Adrian Larson's user avatar
  • 151k
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 ...
Adrian Larson's user avatar
  • 151k
14 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, ...
Adrian Larson's user avatar
  • 151k
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 ...
Alan Morey's user avatar
  • 2,451
13 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, ...
Derek F's user avatar
  • 62.7k
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 */ ...
cropredy's user avatar
  • 72.1k
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(); ...
sfdcfox's user avatar
  • 497k
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 ...
Kris Goncalves's user avatar
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/...
sfdcfox's user avatar
  • 497k
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); } ...
Adrian Larson's user avatar
  • 151k
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 ...
Adrian Larson's user avatar
  • 151k
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 ...
sfdcfox's user avatar
  • 497k
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). ...
Phil W's user avatar
  • 37k
11 votes
Accepted

How to efficiently determine the `name` field of an sobject for dynamic query?

Elegant? Perhaps not, but checking 'Name' first and only iterating as a fallback plan should be more efficient. With every custom object and most standard objects, this shortcut will cut out the ...
Adrian Larson's user avatar
  • 151k
11 votes
Accepted

Most efficient way to switch on SObjectType?

The when values must be literals, so you cannot use String.valueOf as demonstrated in your second example. That said, using String.valueOf is approximately 10% more efficient when using literal string ...
sfdcfox's user avatar
  • 497k
10 votes
Accepted

Best place to declare variables in salesforce

Primary issues at play: Declaration vs. Instantiation Comprehension Variable scope Use of final Access level Declaration vs. Instantiation Declaration and Instantiation are very different questions....
Adrian Larson's user avatar
  • 151k
10 votes

How to avoid instantiating object inside a loop?

In addition to @AdrianLarson's answer, I did a little digging on this for what it's worth using the following code: System.debug('Start: ' + System.now()); List<Contact> contactList = new List&...
Dan Jones's user avatar
  • 7,227
10 votes

Dealing with auto-batching of triggers

However, this 1) doesn't work for before insert since the id doesn't yet exist, and 2) feels legitmately hacky. Are there any better ways to do this? Your approach (Set<Id> recordsProcessed) is ...
Adrian Larson's user avatar
  • 151k
10 votes
Accepted

Effectively profile & speed up slow Lightning components (using Chrome Lightning Inspector)

Client Side Performance Consideration The Chrome lightning debugger has a tree view for the performance thats better than chart view and i find it easier to debug performance bottle necks . Lets ...
Mohith Shrivastava's user avatar
10 votes

Trigger performance and readability - matching text multiple times

I would factor your spam strings out and store them in the database, as a Custom Object, List Custom Setting, or Custom Metadata Type (the choice would depend on who maintains them and whether you ...
David Reed's user avatar
  • 93.2k

Only top scored, non community-wiki answers of a minimum length are eligible