2010/2/3 Joe Strout <[email protected]>:
> blackdog wrote:
>
>> Also, i believe concatenating strings with += is actually very quick
>> in v8 and one doesn't need to add to, and then join an array.
>
> I'd be very surprised and disappointed in 'join' if it is not dramatically
> faster than repeatedly using += in a loop.  Join can calculate the total
> string length, and allocate and copy just once. There's no way += can do
> that.

When you do += in V8 you create a ConsString, which is a tree node
that takes up 20 bytes on 32 bit or 32 bytes on 64 bit.  This is a
relatively fast operation.  The first time you use the string for
things other than consing up results it is converted into a flat
string in much the same way as join would do it.  The ConsString nodes
become garbage which we can collect quite quickly.  Join is subject to
some pretty arcane specifications about how it has to work (eg on
sparse arrays) so that's not a completely trivial operation.

I don't actually know what is faster.  Benchmarks are probably in
order if anyone has time.  Be sure to use a very new V8 version since
there's some recent work in that area and report whether you are on 32
or 64 bit.

-- 
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users

Reply via email to