Salesforce Apex: If else statements vs Switch statements

In this article, we will compare the performance of If else statements vs the Switch statements available in Apex. Switch statements was a long awaited feature in Apex and Salesforce released the Switch statements in the Summer '18 release. This article will help the developers to decide whether to use the traditional If else statements or to go with the newly introduced Switch statement.
I have done a simple test to compare the performance of both. The test involves querying the Account records, along with an inner query to fetch the related contacts and then iterating over them in a loop. The first test uses the If else statements and the second test uses the Switch statement.

Performance of If else statement:
List<Account> accList = [SELECT Name,(SELECT Name FROM Contacts) FROM Account];
System.debug('Start Time: '+Limits.getCpuTime());
Integer i = 0;
for(Account acc: accList){
    if(i == 0){
        System.debug('Account List: '+accList);
        System.debug('Contact List: '+acc.Contacts);
    }else if(i == 1){
        System.debug('Account List: '+accList);
        System.debug('Contact List: '+acc.Contacts);
    }else if(i == 2){
        System.debug('Account List: '+accList);
        System.debug('Contact List: '+acc.Contacts);
    }else if(i == 3){
        System.debug('Account List: '+accList);
        System.debug('Contact List: '+acc.Contacts);
    }else if(i == 4){
        System.debug('Account List: '+accList);
        System.debug('Contact List: '+acc.Contacts);
    }else if(i == 5){
        System.debug('Account List: '+accList);
        System.debug('Contact List: '+acc.Contacts);
    }else if(i == 6){
        System.debug('Account List: '+accList);
        System.debug('Contact List: '+acc.Contacts);
    }else if(i == 7){
       System.debug('Account List: '+accList);
       System.debug('Contact List: '+acc.Contacts);
    }else if(i == 8){
       System.debug('Account List: '+accList);
       System.debug('Contact List: '+acc.Contacts);
    }else if(i == 9){
        System.debug('Account List: '+accList);
        System.debug('Contact List: '+acc.Contacts);
    }else if(i == 10){
        System.debug('Account List: '+accList);
        System.debug('Contact List: '+acc.Contacts);
    }
    i++;
}
System.debug('End Time: '+Limits.getCpuTime());
Here is the result of System debugs from this run:
Run 1:
15:56:05:009 USER_DEBUG [2]|DEBUG|Start Time: 6
15:56:05:014 USER_DEBUG [41]|DEBUG|End Time: 9
Run 2:
16:00:03:009 USER_DEBUG [2]|DEBUG|Start Time: 4
16:00:03:013 USER_DEBUG [41]|DEBUG|End Time: 7
Run 3:
16:00:45:008 USER_DEBUG [2]|DEBUG|Start Time: 5
16:00:45:012 USER_DEBUG [41]|DEBUG|End Time: 8

Performance of Switch statement:
List<Account> accList = [SELECT Name,(SELECT Name FROM Contacts) FROM Account];
System.debug('Start Time: '+Limits.getCpuTime());
Integer i = 0;
for(Account acc: accList){
    switch on i{
when 0{
System.debug('Account List: '+accList);
System.debug('Contact List: '+acc.Contacts);
}
when 1{
System.debug('Account List: '+accList);
System.debug('Contact List: '+acc.Contacts);
}
when 2{
System.debug('Account List: '+accList);
System.debug('Contact List: '+acc.Contacts);
}
when 3{
System.debug('Account List: '+accList);
System.debug('Contact List: '+acc.Contacts);
}
when 4{
System.debug('Account List: '+accList);
System.debug('Contact List: '+acc.Contacts);
}
when 5{
System.debug('Account List: '+accList);
System.debug('Contact List: '+acc.Contacts);
}
when 6{
System.debug('Account List: '+accList);
System.debug('Contact List: '+acc.Contacts);
}
when 7{
System.debug('Account List: '+accList);
System.debug('Contact List: '+acc.Contacts);
}
when 8{
System.debug('Account List: '+accList);
System.debug('Contact List: '+acc.Contacts);
}
when 9{
System.debug('Account List: '+accList);
System.debug('Contact List: '+acc.Contacts);
}
when 10{
System.debug('Account List: '+accList);
System.debug('Contact List: '+acc.Contacts);
}
}
    i++;
}

System.debug('End Time: '+Limits.getCpuTime());
Here is the result of System debugs from this run:
Run 1:
15:57:14:008 USER_DEBUG [2]|DEBUG|Start Time: 5
15:57:14:013 USER_DEBUG [53]|DEBUG|End Time: 9
Run 2:
16:01:51:009 USER_DEBUG [2]|DEBUG|Start Time: 5
16:01:51:014 USER_DEBUG [53]|DEBUG|End Time: 9
Run 3:
16:02:17:009 USER_DEBUG [2]|DEBUG|Start Time: 5
16:02:17:014 USER_DEBUG [53]|DEBUG|End Time: 9

Performance Comparison:
So the If else statement takes 3 ms to execute and the Switch statement takes 4 ms to execute. The traditional if else statement is still better in terms of the performance over the newly released Switch statement.

Comments

  1. 360 Degree Cloud being the Salesforce registered consulting and ISV Partner provides you with the best services on the global level. With over 300+ Salesforce experts working on the related queries, 360 Degree Cloud is serving its customers in the best way possible since the last 8 years. We are among the best Salesforce Consulting Partners providing with the finest services to our clients along with 24*7 tech support. 360 Degree Cloud can provide you with a variety of Salesforce related services and can also be proved beneficial for the growth of your organization in the right terms. We also provide with the exquisite knowledge of Salesforce implementation and consultancy. Unleash the power of Salesforce Consultancy with the help of 360 Degree cloud and explore the infinite pool of opportunities. Your search for the right Salesforce Consulting Company ends here!

    visit-360degreecloud

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Iqra Technology is an IT Solutions and Services Company. We are a salesforce and Microsoft partner company. We aim to provide cost-effective IT services within the customer’s budget range. We scrutinize, design, and develop solutions custom-made for the business necessities. We deliver services in various domains including CRM, ERP, e-commerce, CMS, business intelligence, web development, customized applications, portals, mobile apps, & RPA technologies. We provide IT services starting from $2100 per month and 2 week free trial.

    To know more, kindly visit- Iqra Technology

    ReplyDelete

Post a Comment

Popular posts from this blog

Salesforce Hacks: System.LimitException: Too many queueable jobs added to the queue: 2

Salesforce Lightning: Countdown timer

Building an Org Role Hierarchy component in LWC