The Easiest Way to Save and Share Code Snippets on the web

RegEx vs Split Join Split

actionscript3 | by: theflashbum

posted: Dec, 2nd 2009 | jump to bottom

// This is a simple to test to see how I can add a . prefix to each item in a 
// string list then convert to an array. This test RegEx vs Split Join Split
 
import flash.utils.getTimer;
 
var t:Number;
var styleName:String = "styleA styleB styleC";
var total:int = 100000;
var i:int;
var styles:Array;
 
var totalTime:Number = 0;
 
for (i = 0; i < total; i ++)
{
	t = getTimer();
 
	styles = styleName.replace(/ /g," .").split(" ");
	styles[0] = "."+styles[0];
	t = (getTimer()-t);
 
	totalTime += t;
}
 
trace("Regex Test Average Time", totalTime/total,"ms");
 
for (i = 0; i < total; i ++)
{
	t = getTimer();
	styles = styleName.split(" ").join(" .").split(" ");
	styles[0] = "."+styles[0];
	t = (getTimer()-t);
 
	totalTime += t;
}
 
trace("Split Join Test Average Time",totalTime/total,"ms");
 
//End results
// Regex Test Average Time 0.0066 ms
// Split Join Test Average Time 0.01317 ms
1304 views