Salesforce Performance DML List

Is it better to check if a list is empty before calling DML? Compare the samples below.

List<TestParent__c> parents = new List<TestParent__c>();
// do stuff
insert parents;
                    
List<TestParent__c> parents = new List<TestParent__c>();
// do stuff
if(parents.size() > 0 ){
    insert parents;
}
                    

The first may insert an empty list into the database; the latter checks whether the list is empty, then inserts it into the database.

The good news is even with the first, if the list is empty, there are no DML calls.

The not good news is the first results in about 3x the total time and 4x the cpu time. Even so, the time is very short and probably negligible in most cases. Running the process 1000 times only resulted in a total time of 28 ticks using the more-expensive first code snippet.

Also of note: there was no measurable difference in performance between parents.size() > 0 and !parents.isEmpty().

Also of note: there was no measurable difference if the set was not empty.

 

 

© 2001 – 2023 Object Factory Inc
marker marker marker marker marker