Is String.replace faster than String.split ... String.join in ActionScript 3?

Is it possible to use faster

myString.replace(/foo/g,"bar") 

but not

 myString.split("foo").join("bar") 

for long lines in ActionScript 3? Or are these just two comparable methods to achieve the same result?

+4
source share
3 answers

I used gSkinners PerformaceTest to perform a quick test. I think the difference is minimal at best. I would say that replace() would be the preferred option, because that is what you want to achieve. Using split().join() not so clear in its intent.

 –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– Using replace() (10000 iterations) –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– method...................................................ttl ms...avg ms [function] 57 0.01 –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– Using split().join() (10000 iterations) –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– method...................................................ttl ms...avg ms [function] 61 0.01 –––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– 
+9
source

David R. quoted from the Dirkgently linked blog:

The string.split () construct. join () is the remainder of AS2 days where there was no string.replace (). In AS3, it does not make sense to use .split.join, only people who have a new replacement function will use it.

In addition, the time difference seems minimal according to this blog. So yes, replacing should be a much cleaner way to do this.

+5
source

Here is a good blog post to get you started. But you really have to measure to find out which is faster.

+2
source

All Articles