search
top

replace vs. split and join performance in ActionScript 3

In the previous article inside the source code for the regular expression tester you can find the following line:

var htmlString:String = string.split("\n").join("<br />");

I have found this in an ActionScript 3 example at one time and I remembered this as it is a nice construct. However there is another, more common way to write this:

var htmlString:String = string.replace(/\n/g, "<br />");

So I start questioning myself, which one is faster?

After running some tests the results are: split and join ~ 1100ms, replace ~ 800ms. The test was run on a generated string of 16 mil. characters. So, unless you are planning to do this operation on a large data set the difference is not that big and it does not matter which one you will use. For 100k characters both score around 15ms.

2 Responses to “replace vs. split and join performance in ActionScript 3”

  1. David R says:

    The string.split().join() construct is a leftover from AS2 days, where there was no string.replace(). In AS3, it makes no sense to use .split.join, only people who haven’t learned the new replace function would be likely to use it.

  2. Sakana says:

    Replace is very easy to use for “META TAG” replacement.
    Good to know, thank you for the comparison.

    S.

Leave a Reply

*

top