Skip to content

Consider changing AlphaRouteConstraint to use IndexOfAnyValues instead of Regex #47463

Open
@mitchdenny

Description

@mitchdenny

Currently the AlphaRouteConstraint implementation uses a compiled regex to check to see whether a route value is composed entirely of alpha characters.

We are making a few changes in other areas of the code base to make use of IndexOfAnyValues to determine whether a string is composed soley of a limited set of characters. It got me thinking what the performance difference would look like using IndexOfAnyValues instead of the compiled regex.

c9486b08-cbf9-45ef-8a8b-9ee97329da54

Here is the corresponding code: https://gist.github.com/mitchdenny/3c6da9f9dd0589c424a5bdfd22239676

I brought this up in our teams chat and @JamesNK thought I should share it here and ping @stephentoub and @eerhardt.

NOTE: The difference isn't as dramatic for very large string values (e.g. 400+ characters). I'm not sure if the lines ever intersect and Regex becomes faster or not - probably more of a case of the Regex overheads not mattering as much once you get to that size.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Perfarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-routing

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions