cymhappy / as3corelib

Automatically exported from code.google.com/p/as3corelib
0 stars 0 forks source link

StringUtil.replace Performance Comparison #87

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This is a comparison between using the StringUtil.replace() method vs. 
someStr.split().join() to replace a string 
within a string.

1) as3corelib method:
import com.adobe.utils.StringUtil;
var str:String = "The cow jumped over the moon.";
var newStr:String = StringUtil.replace(str, "moon", "fence");

2) Split/Join method:
var str:String = "The cow jumped over the moon.";
var newStr:String = str.split("moon").join("fence");

This is the code I used to make the comparison:

-------------

import com.adobe.utils.StringUtil;
import flash.utils.*;

var str:String = "The cow jumped over the moon. "
    + "The cow jumped over the moon. "
    + "The cow jumped over the moon. "
    + "The cow jumped over the moon. ";

trace("timer before loop: " + getTimer());

var i:int = 0
var len:int = 50000;
for(i=0; i<len; i++)
{
    // Uncomment the method you'd like to test:

    // 1) as3corelib method
    // StringUtil.replace(str, "moon", "fence");

    // 2) Split/Join method
    // str.split("moon").join("fence");
}

trace("timer after loop: " + getTimer());

-------------

For the as3corelib replace method, the output to the trace window was:
timer before loop: 4
timer after loop: 3145

For the Split/Join replace method, the output to the trace window was:
timer before loop: 3
timer after loop: 324

More details can be found here:
http://www.blogna.org/blog/adobe-flash/actionscript-3-find-and-replace-string-pe
rformance-comparison/

Love the library.  Thanks,
Tim McLeod

Original issue reported on code.google.com by mcleod....@gmail.com on 22 Jan 2009 at 6:08

GoogleCodeExporter commented 9 years ago
Note : need to make sure this doesnt break the unit tests.

Original comment by mikechambers on 21 Sep 2009 at 5:07

GoogleCodeExporter commented 9 years ago
Fixed. Committed revision 109.

Original comment by mikechambers on 22 Sep 2009 at 7:35