I was wondering if there were any javascript idioms that v8 can optimise better than others.
For example, i imagine that having "classes" which don't add/delete fields are probably better, due to v8's hidden classes. 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 think it would be good to have a list best practices to allow javascript generation programs to know how best to take advantage V8.
> I was wondering if there were any javascript idioms that v8 can > optimise better than others.
> For example, i imagine that having "classes" which don't add/delete > fields are probably better, due to v8's hidden classes. > 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 think it would be good to have a list best practices to allow > javascript generation programs to know how best to take advantage V8.
I agree we should have this.
In the mean time "Don't use the 'with' keyword" is the short version.
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.
>> 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.
Erik Corry wrote: > 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.
Well now THAT is really cool.
> I don't actually know what is faster. Benchmarks are probably in > order if anyone has time.
Agreed; intuition about performance is often wrong.
> Erik Corry wrote: > > 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.
> Well now THAT is really cool.
> > I don't actually know what is faster. Benchmarks are probably in > > order if anyone has time.
> Agreed; intuition about performance is often wrong.
When comparing += and Array.join for building a string it might give a more precise picture of the actual cost of building a string if the constructed string is being used afterwards. E.g. include the time used to run a simple RegExp on the string. The string tree built using += will most likely need to be converted into a flat string before complex operations (like RegExp) can be performed on it.
On Thu, Feb 4, 2010 at 00:51, Kiswono Prayogo <kisw...@gmail.com> wrote: > yes, i have tried it too.. ''+=any faster than [any].join('') > on simple operations any+any+any faster than [any,any,any].join('')