How inefficient regEx calls are
Jiten Palaparthi
Consultant (Golang | Rust| Microservices | Docker | Kubernetes | Kafka | Authoring a book). Delivered 200+ trainings in Go, Rust, Kubernetes, Microservices
Please watch below heading 12 seconds animated gif before reading this
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:
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
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.