In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Replacing string by using RegExp or String class only

Get Adobe Flash player
by Fumio 10 Dec 2010
/**
 * Copyright Fumio ( http://wonderfl.net/user/Fumio )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/7CcH
 */

package {
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.utils.getTimer;
	[SWF(width = "240",height = "180")]
	public class StringAndRegExpTest extends Sprite {
		private const COUNT:int = 1000000;
		private const PI:Number = Math.PI;
		private var started:uint;
		private var my_txt:TextField = new TextField();
		private var label_txt:TextField = new TextField();
		public function StringAndRegExpTest() {
			// Creating a TextField for display
			createTextField();
			// Starting test
			var test_str:String = "すもももももももももももいろいろ";
			var find_str:String = "もも";
			var replace_str:String = "みみ";
			useRegExp(test_str, find_str, replace_str);
			use_indexOf(test_str, find_str, replace_str);
			use_split_join(test_str, find_str, replace_str);
		}
		private function useRegExp(test_str:String, find_str:String, replace_str:String):void {
			started = getTimer();
			for (var i:int = 0; i < COUNT; i++) {
				replaceByRegExp(test_str, find_str, replace_str);
			}
			xTrace(getTimer() - started);
		}
		private function use_indexOf(test_str:String, find_str:String, replace_str:String):void {
			started = getTimer();
			for (var i:int = 0; i < COUNT; i++) {
				replaceBy_indexOf(test_str, find_str, replace_str);
			}
			xTrace(getTimer() - started);
		}
		private function use_split_join(test_str:String, find_str:String, replace_str:String):void {
			started = getTimer();
			for (var i:int = 0; i < COUNT; i++) {
				replaceBy_split_join(test_str, find_str, replace_str);
			}
			xTrace(getTimer() - started);
		}
		private function replaceByRegExp(test_str:String, find_str:String, replace_str:String):void {
			var pattern:RegExp = new RegExp(find_str, "g");
			var result_str:String = test_str.replace(pattern, replace_str);
		}
		private function replaceBy_indexOf(test_str:String, find_str:String, replace_str:String):void {
			var numChar:uint = find_str.length;
			var end:uint;
			var result_str:String = "";
			for (var i:uint = 0; -1 < (end = test_str.indexOf(find_str, i)); i = end + numChar) {
				result_str +=  test_str.substring(i, end) + replace_str;
			}
			result_str +=  test_str.substring(i);
		}
		private function replaceBy_split_join(test_str:String, find_str:String, replace_str:String):void {
			var result_str:String = test_str.split(find_str).join(replace_str);
		}
		private function createTextField():void {
			addChild(my_txt);
			addChild(label_txt);
			my_txt.x += 20;
			my_txt.autoSize = TextFieldAutoSize.RIGHT;
			label_txt.autoSize = TextFieldAutoSize.LEFT;
			label_txt.text = "RegExp class:\nString class:\nsplit() and join():";
		}
		private function xTrace(n:int):void {
			my_txt.appendText(String(n) + "\n");
		}
	}
}