Symbols

489 views
Skip to first unread message

Jeremy

unread,
Dec 14, 2010, 1:15:57 PM12/14/10
to v8-users
When is it appropriate to use String::NewSymbol() rather than
String::New()? I assume that symbols are mainly for oft-reused
property names; is this correct? Is obj-
>Set(String::NewSymbol('foo'), value) somehow more efficient than obj-
>Set(String::New('foo'), value)?

Is a Symbol effected differently by the current HandleScope? I often
see people using Persistent symbols for property names and event
identifiers; is this internally more efficient than using string
values?

Erik Corry

unread,
Dec 17, 2010, 4:56:54 AM12/17/10
to v8-u...@googlegroups.com
14. dec. 2010 19.15 skrev Jeremy <jeremy...@gmail.com>:
> When is it appropriate to use String::NewSymbol() rather than
> String::New()?  I assume that symbols are mainly for oft-reused
> property names; is this correct?  Is obj-
>>Set(String::NewSymbol('foo'), value) somehow more efficient than obj-
>>Set(String::New('foo'), value)?

A symbol is just a string that is marked with a bit and which is
automatically canonicalized. There cannot be two symbols with the
same character sequence in the system. There are some places in the
VM where you can only use symbols, not strings. Property names are
one of these. The way it normally works is that if you pass a string
it might be converted inplace into a symbol or the already existing
symbol may be used instead. The second of those takes some time, so
if you know you will be using the same string again and again you may
as well get hold of the symbol from the start. This will save the VM
the trouble of finding the correct symbol every time you pass your
string.

>
> Is a Symbol effected differently by the current HandleScope?  I often
> see people using Persistent symbols for property names and event
> identifiers; is this internally more efficient than using string
> values?

Handles don't care about the difference between symbols and other strings.

>
> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users
>

--
Erik Corry, Software Engineer
Google Denmark ApS - Frederiksborggade 20B, 1 sal,
1360 København K - Denmark - CVR nr. 28 86 69 84

Reply all
Reply to author
Forward
0 new messages