Apex Speed Test – Which One is Fast to Compare String Using ==, equals and equalsIgnoreCase?

Reading Time: 2 minutes

Apex Speed Test Experiment:
This blog is part of Apex Speed Test Experiments.
These experiments check that are we doing something wrong while writing code? What thing is good to write or what is not? Is your single line of code killing the CPU limit?
I am doing experiments whatever I found. You are most welcome here, If you have anything which can be added in these experiments
#CodeShouldNotEatCPU #CPUScientist

Our Experiment:
In this experiment, we will check the performance between == Operator, equals and equalsIgnoreCase methods to compare strings.

You Will Need:
A computer, a browser, a Salesforce Org.

Let’s Start:
We are iterating a loop about 50,000 times. In the loop, we are checking if condition with different approaches.

Experiment 1:
In this experiment, We are using == operator in the loop.

Open the developer console. Now Open Execute Anonymous Window. (This option present in Debug menu.)
Copy this code ApexSpeedExperiment_3.runExperiment1(); and run it from developer console. I called this method 5 times and here are the results.

 

Result: Average time taken is 115.6 Milliseconds.

Experiment 2:
In this experiment, We are using string.equals() method in the loop.

Open the developer console. Now Open Execute Anonymous Window. (This option present in Debug menu.)
Copy this code ApexSpeedExperiment_3.runExperiment2(); and run it from developer console. I called this method 5 times and here are the results.

Result: Average time taken is 247.2 Milliseconds.

Experiment 3:
In this experiment, We are using string.equalsIgnoreCase() method in the loop.

Open the developer console. Now Open Execute Anonymous Window. (This option present in Debug menu.)
Copy this code ApexSpeedExperiment_3.runExperiment3(); and run it from developer console. I called this method 5 times and here are the results.

 

Result: Average time taken is 253.8 Milliseconds.

Conclusion:
According to our experiments, the == operator is taking less than half-time from String.equals() and String.equalsIgnoreCase() methods.

1. We can use == operator instead of String.equalsIgnoreCase() method. As seems like they are working for the same purpose.
2. We can not use == operator instead of String.equals() method. As they both work differently. (Case-Insensitive andCase-Sensitive)

Thank You for taking part in the Apex Speed Test Experiment. If you have any other experiment in mind. Please let me know. I will conduct an experiment and will share the blog here.

2 thoughts on “Apex Speed Test – Which One is Fast to Compare String Using ==, equals and equalsIgnoreCase?

  • November 14, 2018 at 5:40 pm
    Permalink

    In conclusion the 1st &. 2nd point have a typo/incorrect. == Is case insensitive & so is string.equalsIgnoreCase but string.isequal is case sensitive

    Reply
    • November 15, 2018 at 6:26 am
      Permalink

      Thank You. Yes you are right. I corrected it. Few drawbacks while writing blog in too late night. 🙂

      Reply

Leave a Reply