I had a good experience at the speed of regex in JS
.
And I decided to make a small comparison. I ran the following code:
var str = "A regular expression is a pattern that the regular expression engine attempts to match in input text.";
var re = new RegExp("t", "g");
console.time();
for(var i = 0; i < 10e6; i++)
str.replace(re, "1");
console.timeEnd();
The result: 3888.731ms.
Now in C#
:
var stopwatch = new Stopwatch();
var str = "A regular expression is a pattern that the regular expression engine attempts to match in input text.";
var re = new Regex("t", RegexOptions.Compiled);
stopwatch.Start();
for (int i = 0; i < 10e6; i++)
re.Replace(str, "1");
stopwatch.Stop();
Console.WriteLine( stopwatch.Elapsed.TotalMilliseconds);
Result: 32798.8756ms !!
Now, I tried re.exec(str);
vs Regex.Match(str, "t");
: 1205.791ms VS 7352.532ms in favor of JS
.
Is massive text processing "Not suitable" subject to be done in .net
?
UPDATE 1 same test with [ta]
pattern (instead t
literal):
3336.063ms in js VS 64534.4766!!! in c#.
another example:
console.time();
var str = "A regular expression is a pattern that the regular expression engine attempts 123 to match in input text.";
var re = new RegExp("\\d+", "g");
var result;
for(var i = 0; i < 10e6; i++)
result = str.replace(str, "$0");
console.timeEnd();
3350.230ms in js, vs 32582.405ms in c#.