Re: Javascript String Concatenations
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Javascript String Concatenations

From: Lasse Reichstein Nielsen <lrn@hotpop.com>
Date: Wed Nov 30 2005 - 21:42:11 CET

"web.dev" <web.dev.cs@gmail.com> writes:

> I would argue that test study is not conclusive. True, string
> concatenations can be slow, but how you use them will also affect
> performance. In the test study show, you're building a table.
> Obviously it would be slow.

Why would it be slower than some other string of similar length?

> Have you considered other factors? The code is no doubt
> inefficient.

Apart from the string contcatenation, there are nothing that really
makes a difference.

> For example, it is using two for loops! That's a O(n^2).

Well, no. The table has COLS*ROWS cells. Whether in one loop or
two, you have to create that many td-elements. If anything, that
number should be your "n" for complexity analysis.

> Don't forget the initialization of a for loop.

Yes, it has overhead, but it's negligable.
A coprehensive test would also do a run of the loop without any
content, to find the overhead of that, but it is linear in n,
and with O(n^2) time for string doing n string concatenations,
that's quickly ignorable.

> Oh yeah, did you miss the post increment? Would be more efficient
> if it was a pre-increment instead.

You have absolutely no way of knowing that. The interpreter can easily
optimize a post increment where the value isn't used into a simple
increment. And again, even if it doubles the time taken for the increment,
it is still only a constant factor.

> Unless you have written an optimized code, that study does not
> accurately show the performance impact of string concatenations just
> because there are other factors that are heavily weighed in (to me
> anyways).

It doesn't really matter whether the surrounding code is optimized,
as long as you time it independently and subtract that from the total.

/L

-- 
Lasse Reichstein Nielsen  -  lrn@hotpop.com
 DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
  'Faith without judgement merely degrades the spirit divine.'
Received on Sat Dec 3 04:33:58 2005