7

I have to concatenate a bunch of Strings in Javascript and am searching for the fastest way to do so. Let's assume that the Javascript has to create a large XML-"file" that, naturally, consists of many small Strings. So I came up with:

    var sbuffer = [];
    for (var idx=0; idx<10000; idx=idx+1) {
        sbuffer.push(‘<xmltag>Data comes here... bla... </xmltag>’);
    }
    // Now we "send" it to the browser...
    alert(sbuffer.join(”));

Do not pay any attention to the loop or the other "sophisticated" code which builds the example.

My question is: For an unknown number of Strings, do you have a faster algorithm / method / idea to concatenate many small Strings to a huge one?

7 Answers 7

15

The question JavaScript string concatenation has an accepted answer that links to a very good comparison of JavaScript string concatenation performance.

Edit: I would have thought that you could eek out a little more performance by using Duff's device as the article suggests.

1
  • Hello Sam, I saw that post before but my question is slightly different in behave that I really am asking if someone has a faster solution and not only try to compare concatenation versus join. Who knows? Perhaps there is an other solution out there that is faster for huge Strings.
    – Georgi
    Sep 30, 2008 at 14:57
13

Changing the line:

sbuffer.push(‘Data comes here... bla... ’);

to

sbuffer[sbuffer.length] = ‘Data comes here... bla... ’;

will give you 5-50% speed gain (depending on browser, in IE - gain will be highest)

Regards.

1
  • 1
    Do you have any source for stats on that? I would have figured the penalty for checking the length on every iteration (either in push, or array.length) would work out the same. I'm certainly aware that IE's String concatenation speed leaves much to be desired though.
    – scunliffe
    May 15, 2009 at 20:21
1

I think you are quite close to the optimum. YMMV, a great deal of speed is gained or lost within the JavaScript engine of the host process (e.g. browser).

3
  • Yup, actually Javascript "performance" has the big advantage to run on many client machines and not only on one server - divide et impere ;-). But with the client-scripting getting more complex, performance get's an issue there, too. Question: What does YMMV mean?
    – Georgi
    Sep 30, 2008 at 14:53
  • @Georgi: Your Mileage May Vary Sep 30, 2008 at 14:57
  • "Your mileage may vary". Google is your friend, too. ;-)
    – Tomalak
    Sep 30, 2008 at 14:58
1

I think that pushing the strings onto an array and then joining the array is the fastest technique for string concatenation in JavaScript. There is some supporting evidence in this discussion about W3C DOM vs. innerHTML. Note the difference between the innerHTML 1 and innerHTML 2 results.

1

As far as I know, your algorithm is good and known as a performant solution to the string concatenation problem.

0

Beware of IE bad garbage collector! What do you suppose to do with your array after using? Probably it will get GC'd?

You can gain perfornace on concatenating with joins, and then lose on post-GC'ing. On the other hand if you leave an array in scope all the time, and NOT reuse it, that can be a good solution.

Personally I'd like the simpliest solution: just to use += operator.

0
0

You might get a little more speed by buffering.

1
  • Sorry this doesn't help because the number of Strings to be concatenated is not known ("For an unknown number of Strings..."). I thought of buffering too, but this will heavily increase the memory footprint and garbage collection.
    – Georgi
    Oct 1, 2008 at 16:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.