Lately I’ve been seeing a lot of AS3 code that uses regular expressions where the normal methods of the String class would seem to suffice. It seems common among programmers of all languages to catch on to new trends even when they do not particularly apply to the task at hand. For example, a new C++ or Java programmer may use templates or generics even for classes where only one data type will likely ever be used. I don’t know why programmers do this and I won’t attempt to tackle such a topic. Instead, I’ll do a test to show why this is a bad idea from a performance perspective.

First of all, you should probably not be using regular expressions in any performance critical area anyhow. They are clearly an advanced feature with a lot going on behind the scenes, much of it extremely generalized and therefore not especially optimized for task. This is why regular expressions are typically used for tasks like form validation or one time document searches and not for tasks that occur every frame, even though the latter is probably fine in moderation.

Secondly, it is very likely that regular expressions– written in native code in the Flash Player– outperform the String class’ methods for anything complicated. Further, they are most certainly more concise and arguably more readable. The following test app is simply to show the performance of simple regular expressions against the equivalent String.indexOf():

package
{
	import flash.display.*;
	import flash.text.*;
	import flash.utils.*;
 
	/**
	*   An app to test the performance of simple regular expressions against simple strings
	*   @author Jackson Dunstan
	*/ 
	public class SimpleRegExpPerformanceTest extends Sprite
	{
		public function SimpleRegExpPerformanceTest()
		{
			var logger:TextField = new TextField();
			logger.autoSize = TextFieldAutoSize.LEFT;
			addChild(logger);
			function log(msg:*): void { logger.appendText(msg+"\n"); }
 
			const needleString:String = "apple";
			const needleRegExp:RegExp = /apple/;
			const haystack:String = "Fruits like apples are tasty.";
			const NUM_ITS:int = 100000;
			var i:int;
			var beforeTime:int;
 
			beforeTime = getTimer();
			for (i = 0; i < NUM_ITS; ++i)
			{
				haystack.indexOf(needleString);
			}
			log("String.indexOf(): " + (getTimer()-beforeTime));
 
			beforeTime = getTimer();
			for (i = 0; i < NUM_ITS; ++i)
			{
				haystack.lastIndexOf(needleString);
			}
			log("String.lastIndexOf(): " + (getTimer()-beforeTime));
 
			beforeTime = getTimer();
			for (i = 0; i < NUM_ITS; ++i)
			{
				needleRegExp.test(haystack);
			}
			log("RegExp.test(): " + (getTimer()-beforeTime));
 
			beforeTime = getTimer();
			for (i = 0; i < NUM_ITS; ++i)
			{
				needleRegExp.exec(haystack);
			}
			log("RegExp.exec(): " + (getTimer()-beforeTime));
		}
	}
}

The results I get are:

Environment String.lastIndexOf() String.indexOf() RegExp.test() RegExp.exec()
3.0 Ghz Intel Core 2 Duo, 4 GB, Windows XP 6 6 142 139

This clearly shows the regular expression equivalents– test() and exec()— lagging behind plain old String.indexOf() and String.lastIndexOf() by a factor of about 23. It may seem clever to use regular expressions, but it sure does carry a massive performance penalty. Again, this is only a concern if you are using simple regular expressions in a performance critical area. If you can think of any such places in your own code, consider revising them with a simple String method to save some cycles.