I'm converting a Java application that parses log files using various regex patterns. Part of the application feeds a number of log messages into a series of regexes to see which messages match which regex. In my test, I've got 68 messages feeding into 279 regexes. Each message takes about
6 seconds to run through the "hasMatch" function for all the regexes. 6 seconds times 68 messages means it is taking over
6 minutes for this process. This is not acceptable. Here is the essential Dart code:
RegExp re = new RegExp(pattern);
bool matched = re.hasMatch(message);
I decided to see how slow JavaScript was. I replaced the Dart RegExp code with a call to a JavaScript function. Each message now only takes about 0.06 seconds to run through the "test" function for all the regexes. 0.06 seconds times 68 messages means it is taking about 4 seconds for this process. For an interactive program, this is acceptable. Here is the essential Dart and JavaScript code:
var matched = new JsObject(context['jsRegex']).callMethod('test', [message, pattern]);
JavaScript:
var jsRegex = function() {
this.test = function(message, pattern) {
var re = new RegExp(pattern);
var hasMatch = re.test(message);
return hasMatch;
};
};
If I'm not doing anything wrong, then the Dart team really needs to work on RegExp performance.