singhabhisek
singhabhisek

Reputation: 57

Groovy Script Performance Contains vs Matches

I am new to groovy language. I am using groovy code to check the response from the http url if it is valid. From performance perspective, what is typically advisable to use - contains() or matches() which would allow me to validate if I got correct response.

Upvotes: 0

Views: 1562

Answers (1)

injecteer
injecteer

Reputation: 20699

In Java contains() or in operator of Groovy checks for exact match on plain string basis and basically is a shortcut for -1 != str.indexOf('aaa').

On the other hand, matches() in java or ==~ and =~ in groovy introduce Regex Patter matching overhead and are somewhat slower. This is the price you are paying for power and flexibility of regex.

Going back to your use case, I think the performance cost of matches() over contains() is negclectible, as the http request takes magnitudes of time longer to execute.

In this case I would go for solution with greater readability and flexibility, which would be a regex way. On the other hand, if you really want exact match, you should stick with contains.

Beyond the abovementioned, if you really want the last drop of performance, you should run the load tests of both methods yourself.

Upvotes: 1

Related Questions