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?

Mark Biek
  • 146,731
  • 54
  • 156
  • 201
Georgi
  • 4,402
  • 4
  • 24
  • 20

7 Answers7

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.

Community
  • 1
  • 1
Sam Hasler
  • 12,344
  • 10
  • 72
  • 106
  • 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 '08 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.

Sergey Ilinsky
  • 31,255
  • 9
  • 54
  • 56
  • 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 '09 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).

Tomalak
  • 332,285
  • 67
  • 532
  • 628
  • 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 '08 at 14:53
  • @Georgi: Your Mileage May Vary – Chris Cudmore Sep 30 '08 at 14:57
  • "Your mileage may vary". Google is your friend, too. ;-) – Tomalak Sep 30 '08 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.

John Topley
  • 113,588
  • 46
  • 195
  • 237
1

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

Barth
  • 15,135
  • 20
  • 70
  • 105
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.

Thevs
  • 3,189
  • 2
  • 20
  • 32
0

You might get a little more speed by buffering.

Community
  • 1
  • 1
Andrew Hedges
  • 21,688
  • 16
  • 67
  • 79
  • 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 01 '08 at 16:26