# Apex Speed Test – Which One is Fast to Compare String Using ==, equals and equalsIgnoreCase? 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.

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

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

• November 15, 2018 at 6:26 am

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

• January 15, 2019 at 4:55 pm

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!

•
{"ft_lang_label":"__label__en","ft_lang_prob":0.9152554,"math_prob":0.8147621,"size":5369,"snap":"2019-51-2020-05","text_gpt3_token_len":1211,"char_repetition_ratio":0.13196644,"word_repetition_ratio":0.22789116,"special_character_ratio":0.23263177,"punctuation_ratio":0.14108674,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9552027,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],"im_url_duplicate_count":[null,5,null,2,null,2,null,5,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-15T03:19:41Z\",\"WARC-Record-ID\":\"<urn:uuid:d7a38778-e2e2-440e-9a54-d6eecb353b56>\",\"Content-Length\":\"87260\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:92ef07bf-2639-4414-a5ab-ffeb51aec0f1>\",\"WARC-Concurrent-To\":\"<urn:uuid:016ed519-74d9-4c4a-9066-3cc2bbf1f940>\",\"WARC-IP-Address\":\"166.62.6.78\",\"WARC-Target-URI\":\"http://www.0to1code.com/apex-speed-test-which-one-is-fast-to-compare-string-using-equals-and-equalsignorecase/\",\"WARC-Payload-Digest\":\"sha1:P2DKXAMSMJO6KR5NS5ZXFWQWD67FNOZC\",\"WARC-Block-Digest\":\"sha1:AK45I7OSHCILI6X7K5ULHMGSBK7JTWXS\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575541301014.74_warc_CC-MAIN-20191215015215-20191215043215-00277.warc.gz\"}"}