V8 Optimizations?

105 views
Skip to first unread message

blackdog

unread,
Feb 3, 2010, 8:41:10 AM2/3/10
to v8-users

Hi,

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.

bd

Erik Corry

unread,
Feb 3, 2010, 8:43:54 AM2/3/10
to v8-u...@googlegroups.com
2010/2/3 blackdog <black...@gmail.com>:

I agree we should have this.

In the mean time "Don't use the 'with' keyword" is the short version.

--
Erik Corry

Joe Strout

unread,
Feb 3, 2010, 9:17:20 AM2/3/10
to v8-u...@googlegroups.com
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.

Best,
- Joe

Erik Corry

unread,
Feb 3, 2010, 9:35:26 AM2/3/10
to v8-u...@googlegroups.com
2010/2/3 Joe Strout <j...@strout.net>:

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.

Joe Strout

unread,
Feb 3, 2010, 10:58:06 AM2/3/10
to v8-u...@googlegroups.com
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.

Best,
- Joe


blackdog

unread,
Feb 3, 2010, 11:43:36 AM2/3/10
to v8-users
someone on #node.js did a test, if i remember correctly += was quicker

Kiswono Prayogo

unread,
Feb 3, 2010, 6:51:26 PM2/3/10
to v8-u...@googlegroups.com
yes, i have tried it too.. ''+=any faster than [any].join('')
on simple operations any+any+any faster than [any,any,any].join('')

--
Regards,
Kiswono P
GB

Søren Gjesse

unread,
Feb 4, 2010, 2:18:40 AM2/4/10
to v8-u...@googlegroups.com
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.

Regards,
Søren


Kung fu tzu

unread,
Feb 8, 2010, 11:44:15 AM2/8/10
to v8-users
Am made a simple benchmark. It can be run in all modern browsers to
test them for ConsString support or its analog.

http://lib.programica.ru/lib/tests/benchmarks/cons-string-vs-array-join.html

Reply all
Reply to author
Forward
0 new messages