views:

2978

answers:

7

Hello,

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?

+1  A: 

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
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
Your Mileage May Vary
insin
@Georgi: Your Mileage May Vary
chris
"Your mileage may vary". Google is your friend, too. ;-)
Tomalak
Who's Google? *scnr* - thanks!
Georgi
+10  A: 

This question: JavaScript string concatenation would have been suggested when you entered the title for your question. The accepted answer 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.

Sam Hasler
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
It might be worth stating that in the question. It's always worth explaining what research you've already done (especially if there are similar questions already on Stackoverflow) and how it didn't answer your question.
Sam Hasler
Wonderful link, thanks!
stefano m
+1  A: 

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
+1  A: 

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

Barth
+8  A: 

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
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
A: 

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
I wonder what's wrong with my answer?!
Thevs
A: 

You might get a little more speed by buffering.

Andrew Hedges
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