How inefficient regEx calls are

How inefficient regEx calls are

Please watch below heading 12 seconds animated gif before reading this

No alt text provided for this image

By now, most of you know a project called readyGo. While I was trying to improve the performance of the project by bench-marking key methods/functions and then refactoring them, I came across many scenarios where I could have written better code.One among that scenarios is bench-marking a validation(IsValidIdentifier).As the name suggests , it is to validate whether a given name in readyGo Configuration (model name or field name) is valid identifier according to Go.

As all of you know , these are general checks to validate an identifier in most of the programming languages.

  • It must start with either underscore( _ ) or any of the characters from ranges [a -z] and [A-Z]
  • There must not be any which space in the string
  • All subsequent characters after the first character must not consist of any special characters other than ( _ ), [a-z],[A-Z] or [0-9]

Take a look at the below code:

Carefully look at the code. IsValid method one with regEx and the other one with conditions

Take a look at the below bench marking result. The original result watch in first animated gif. This is a tabular form for better understanding

No alt text provided for this image
Hope you understand how costly regEx is. Avoid regEx as much as possible :)

To know more about readyGo project , please read readyGo Wiki . Access readyGo readyGo Source . Star the project in GitHub.Com , fork it and contribute to it.

I am always reachable in LinkedIn or at my twitter handle @jiten_1981.

Thank you. Happy learning.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了