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?
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.
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.
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
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With