>> String.concat is faster than "+". Firefox and Opera optimize the "+="
>> operator, but most of the concatenation would be happening with "+" on 
>> the right side and generating lots of temp strings, unless you did this:
>
> Hey Dave, cool, I didn't know of String.concat. Would be interesting 
> to know, if it's faster then the method using an Array and join!?

You can make a benchmark that shows any of them to be a winner.

The advantage of "+=" is that it's a primitive string operation and
(supposedly in FF and Opera) has been optimized to avoid extra string
copies. The "+" generates copies but it's still a primitive operation so
it's fast for short strings.

Array.push followed by a final Array.join has method call overhead as well,
although you can eliminate most of it by using a[a.length] instead of a.push
to add strings. Its big advantage is that you don't need all the string
parts at once, so you can put them together in a loop for example. It tends
to perform better than "+" for large numbers of long strings.

String.concat has the overhead of a method call, but avoids extra temps. It
is slower than "+" or "+=" if all you are doing is concatenating two or
three short strings, but it can save time/memory with long strings. The
limitation is that you have to have all the strings available at once to
pass them to concat. It does have one big advantage that it will save you
from these kind of silent errors when you forget a trailing plus sign:

 var str = "this is a "
    "string I want to join";
 alert(str);

In reality we should all save the brainpower, use + and += most of the time,
and look elsewhere for performance increases. :) 

IEBlog had a post recently about this, with benchmarks:

http://blogs.msdn.com/ie/archive/2006/11/16/ie-javascript-performance-recomm
endations-part-2-javascript-code-inefficiencies.aspx



_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to