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

Reading Time: 3 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.

(Edit 15 Jan 2019) NOTE: According to a discussion on twitter with Aidan Harding. It turns out that the above one is running fast because of a concept called Constant Folding.

I rerun the experiment 1 and experiment with slightly code changes and found out that experiment 3 is faster. Or I can say they both are taking almost the same time.

Please refer to this twitter thread.

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

Open the developer console. Now Open Execute Anonymous Window. s 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.

(Edit 15 Jan 2019) According to our experiments, the == operator is taking more time from String.equals() and String.equalsIgnoreCase() methods.

1. We can useString.equalsIgnoreCase() instead of == operator . 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.

4 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
  • January 15, 2019 at 4:55 pm
    Permalink

    Hi,

    Following up on my Twitter comment…

    Where you have written ‘Test’==’Test’, many compilers would use Constant Folding (https://en.wikipedia.org/wiki/Constant_folding) to turn that into just “true” at compile-time. It doesn’t need to run the code to see that ‘Test’==’Test’. So, it may be that your test is not actually even running any == comparisons, it might just be doing a compile-time evaluation, then going around the loop 50000 times.

    You could attempt to check for that by running “if( true ){}” inside a loop and see what timing you get.

    Even if you write:

    String s1 = ‘Test’;
    String s2 = ‘Test’;
    for(Integer i=0; i<50000 ;i++){
    if(s1==s2 ){}
    }

    Then, you still can't be sure. A sufficiently clever compiler may be able to deduce that s1 and s2 are still constants. Even if that is not deduced, the string 'Test' will end up being stored once on the heap. Then s1 and s2 will both point to the same address. And an efficient way to write the == comparator is to first check the memory addresses of the things you're comparing. If they are equal, then you're done. If not, you need to actually check the data. If this were the case, you might get very different results for comparing string that are equal versus those that are not equal.

    Since we don't know how the Apex compiler does any of that, I can't tell you what the answers are. But, in general, benchmarking is hard!

    Reply
    • January 15, 2019 at 6:32 pm
      Permalink

      I have added a few more insight on the same and added our discussion into the blog. Seems like experiment 1 and experiment 3 are almost the same but now experiment 3 is winning.
      Thank you for letting know about the concept of Constant Folding.

      Reply

Leave a Reply