Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is it any faster to use

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

rather than

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

for long strings in ActionScript 3? Or are they just two comparable methods of achieving the same result?

like image 630
Reuben Avatar asked Oct 29 '09 09:10

Reuben


People also ask

What does splitting a string do?

Split is used to break a delimited string into substrings. You can use either a character array or a string array to specify zero or more delimiting characters or strings. If no delimiting characters are specified, the string is split at white-space characters.

How do you split a string into 3 strings in Python?

Python split() method is used to split the string into chunks, and it accepts one argument called separator. A separator can be any character or a symbol. If no separators are defined, then it will split the given string and whitespace will be used by default.


1 Answers

I used gSkinners PerformaceTest to run a quick test on this. I think the difference is minimal at best. I would say that replace() would be the preferred option purely because that is what you want to achieve. Using split().join() is not as 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
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
like image 67
enzuguri Avatar answered Sep 21 '22 19:09

enzuguri