{"action":"created","issue":{"url":"https://api.github.com/repos/code-cracker/code-cracker/issues/297","labels_url":"https://api.github.com/repos/code-cracker/code-cracker/issues/297/labels{/name}","comments_url":"https://api.github.com/repos/code-cracker/code-cracker/issues/297/comments","events_url":"https://api.github.com/repos/code-cracker/code-cracker/issues/297/events","html_url":"https://github.com/code-cracker/code-cracker/issues/297","id":63550570,"number":297,"title":"Regex performance: use static Regex.IsMatch","user":{"login":"akamud","id":954102,"avatar_url":"https://avatars.githubusercontent.com/u/954102?v=3","gravatar_id":"","url":"https://api.github.com/users/akamud","html_url":"https://github.com/akamud","followers_url":"https://api.github.com/users/akamud/followers","following_url":"https://api.github.com/users/akamud/following{/other_user}","gists_url":"https://api.github.com/users/akamud/gists{/gist_id}","starred_url":"https://api.github.com/users/akamud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/akamud/subscriptions","organizations_url":"https://api.github.com/users/akamud/orgs","repos_url":"https://api.github.com/users/akamud/repos","events_url":"https://api.github.com/users/akamud/events{/privacy}","received_events_url":"https://api.github.com/users/akamud/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/code-cracker/code-cracker/labels/2%20-%20Working","name":"2 - Working","color":"CCCCCC"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2015-03-22T16:31:06Z","updated_at":"2015-03-22T23:34:25Z","closed_at":null,"body":"We could diagnose code using Regex in a way that gives poor performances.\r\n\r\nCreating a new Regex every time will give you a bad performance:\r\n\r\n```C#\r\npublic static bool IsValidCurrency(string currencyValue)\r\n{\r\n  string pattern = @\"\\p{Sc}+\\s*\\d+\";\r\n  Regex currencyRegex = new Regex(pattern);\r\n  return currencyRegex.IsMatch(currencyValue);\r\n}\r\n```\r\n\r\nIf this method is called multiple times, you should be using the [static `Regex.IsMatch`](https://msdn.microsoft.com/en-us/library/sdx2bds0%28v=vs.110%29.aspx) method, as that can be [cached](https://msdn.microsoft.com/en-us/library/gg578045(v=vs.110).aspx) and won't be interpreted every time you call the method, it will give you better performance.\r\nThis is quite common on email address validation during registration, or validating user input (like the currency example above or Zip codes).\r\n\r\nCode fix:\r\n```C#\r\npublic static bool IsValidCurrency(string currencyValue)\r\n{\r\n  string pattern = @\"\\p{Sc}+\\s*\\d+\";\r\n  return Regex.IsMatch(currencyValue, pattern); \r\n}\r\n```\r\nI created a [project](https://github.com/akamud/RegexPerformanceExamples/blob/master/RegexPerformanceExamples/Program.cs) showing the difference in performance, testing this case with only 10 runs shows that instance method is almost 3 times slower.\r\n```\r\nInstance took 0.000639 seconds\r\nStatic took   0.000240 seconds\r\n```\r\n\r\nRunning this 100,000 times gives these results:\r\n```\r\nInstance took 0.3786505 seconds\r\nStatic took   0.0717608 seconds\r\n```\r\n\r\nI'm sure there are [more cases](http://blogs.msdn.com/b/bclteam/archive/2010/06/25/optimizing-regular-expression-performance-part-i-working-with-the-regex-class-and-regex-objects.aspx) of improving Regex performance that we could diagnose, but this one is pretty straightforward. What do you think?\r\n\r\nDiagnostic Id: `CC0081`\r\nSeverity: `Warning`\r\nStyle: `Performance`\r\n"},"comment":{"url":"https://api.github.com/repos/code-cracker/code-cracker/issues/comments/84732903","html_url":"https://github.com/code-cracker/code-cracker/issues/297#issuecomment-84732903","issue_url":"https://api.github.com/repos/code-cracker/code-cracker/issues/297","id":84732903,"user":{"login":"giggio","id":334958,"avatar_url":"https://avatars.githubusercontent.com/u/334958?v=3","gravatar_id":"","url":"https://api.github.com/users/giggio","html_url":"https://github.com/giggio","followers_url":"https://api.github.com/users/giggio/followers","following_url":"https://api.github.com/users/giggio/following{/other_user}","gists_url":"https://api.github.com/users/giggio/gists{/gist_id}","starred_url":"https://api.github.com/users/giggio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/giggio/subscriptions","organizations_url":"https://api.github.com/users/giggio/orgs","repos_url":"https://api.github.com/users/giggio/repos","events_url":"https://api.github.com/users/giggio/events{/privacy}","received_events_url":"https://api.github.com/users/giggio/received_events","type":"User","site_admin":false},"created_at":"2015-03-22T23:34:25Z","updated_at":"2015-03-22T23:34:25Z","body":"I am fine with that. It also makes the analyzer simpler. But I would still offer the 3 code fixes, they would be great."}}