I'm facing a problem with Regex performance in C#.
I need to replace on a very large string (270k charachters, don't ask why..). The regex matches about 3k times.
private static Regex emptyCSSRulesetRegex = new Regex(@"[^\};\{]+\{\s*\}", RegexOptions.Compiled | RegexOptions.Singleline);
public string ReplaceEmptyCSSRulesets(string css) {
return emptyCSSRulesetRegex.Replace(css, string.Empty);
}
The string I pass to the method looks something like this:
.selector-with-statements{border:none;}.selector-without-statements{}.etc{}
Currently the replace process takes up 1500ms in C#, but when I do exactly the same in Javascript it only takes 100ms.
The Javascript code I used for timing:
console.time('reg replace');
myLargeString.replace(/[^\};\{]+\{\s*\}/g,'');
console.timeEnd('reg replace');
I also tried to do the replacing by looping over the matches in reverse order and replace the string in a StringBuilder. That was not helping.
I'm surprised by the performance difference between C# and Javascript in this case, and I think there I'm doing something wrong but I cannot think of anything.
matches())itself is a time consuming process. It involves synchronization and other time consuming stuff.\sto[\f\n\r\t\v\u00A0\u2028\u2029]or usingRegexOptions.ECMAScript. There are differences on how character classes are handled in JS and in C#