3

Need some understanding regarding performance when using a single if vs ternary operator.

I create a wrapper instance wherein I initialize its variables. Eg below :

public class MyWrapperClass{
    public string sUserName;
    
    public MyWrapperClass(){
        this.sUserName = '';
    }
}

Now, while I assign the values returned from back-end to the wrapper variables, I use a single if to check if the value returned from the back-end is not blank. Below is what I do:

if(String.isNotBlank(myObj.myField__c)){
    myWrapperInstance.sUserName  = myObj.myField__c;
}

I don't have an else condition here because I've already initialized the the variable as an empty string and hence if no value exists for the field, then my variable would hold an empty string.

However, I need to understand if it makes sense to use below:

myWrapperInstance.sUserName = String.isNotBlank(myObj.myField__c) ? myObj.myField__c : '';

OR

myWrapperInstance.sUserName = myObj.myField__c != null ? myObj.myField__c : '';

I need help understanding what is the better of the two from performance standponint?

1
  • 4
    It's not worth trying to chase such a minute micro-optimization (if there is any difference at all).
    – Derek F
    Commented Aug 19, 2020 at 21:35

1 Answer 1

6

These forms:

myWrapperInstance.sUserName = String.isNotBlank(myObj.myField__c) ? myObj.myField__c : myObj.myField__c;

and

myWrapperInstance.sUserName = myObj.myField__c != null ? myObj.myField__c : myObj.myField__c;

are apples-to-oranges. String.isNotBlank() does not simply check for null, but also for empty and all-whitespace strings, which is more computationally expensive than checking for null. Use the one you need for your specific requirements, since they are not performance-equivalent.

Additionally, both of these should be optimized away as written if the Apex compiler is moderately clever, because the comparison results in the same output. (I suspect that's just a mistake in your example, though).

As for whether the ternary operator or an if statement is faster: as Derek F. commented, it doesn't matter. The performance impact, if any - and again, if the Apex compiler is clever, there shouldn't be any different at all - will both be completely swamped by other performance characteristics of your application, and undefined/subject to change in future Salesforce releases. Don't worry about it.

3
  • yes, that was a typo in the example, I've updated the question. And, thanks for your inputs, just from understanding point I wanted to know which of the two approaches ( when using String.isNotBlank() in both cases) is better even if the difference is next to nothing. Commented Aug 19, 2020 at 22:30
  • There is absolutely no difference in performance (if vs ternary). One is simply less typing.
    – sfdcfox
    Commented Aug 19, 2020 at 22:55
  • I'm a ternary man --- one fewer line of vertical code
    – cropredy
    Commented Aug 20, 2020 at 1:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .