Mike Mike - 18 days ago 5
Java Question

regex VS Contains. Best Performance?

I want to compare an URI String over different patterns in java and I want fastest code possible.

Should I use :

if(uri.contains("/br/fab") || uri.contains("/br/err") || uri.contains("/br/sts")


Or something like :

if(uri.matches(".*/br/(fab|err|sts).*"))


Note that I can have a lot more uri and this method is called very often.

What is the best answer between my choices ?

Answer

They're both fast enough to be over before you know it. I'd go for the one that you can read more easily.

Comments