I cannot answer your question specifically about how v8 internals work with 
caching property names, but I might be able to help with general 
information about Javascript objects.

On Tuesday, January 15, 2013 9:18:04 AM UTC-8, ajlopez wrote:
>
> Hi people!
>
> I will create in project (Node.js) a lot of object withÑ
>
> var obj = { author: aValue };
>

The Javascript engine will allocate memory every time that line is called 
to create a new object, which contains a property called 'auithor' which 
has a value of aValue. It does not matter where in the application this is 
done, every invocation will create a new object. I 'think' the author 
symbol for the property name will need to be created also, which is  a 
string for the property name.


> maybe in different parts of the application (not in the same line/place)
>
> the symbol/string "author", is allocated only once? or is allocated only 
> once per line of code?
> Maybe I should ask: the property names are interned?
>

I think every place the string "author" exists will be a new instance of 
the string.  I could be wrong on this due to v8 internal optimization. I 
would think the same would go for the property name symbol.  Because in 
most situation they are interchangeable, except when they're not :-/.

For example obj["author"], obj.author, and var propName = "author"; 
obj[propName] all perform the same operation.


> Another related question: if i do:
>
> obj[propname] = aValue;
>
> many times, in many places, (maybe thousands of times), and each time, the 
> propname points to the string "author", then the property name is stored 
> only once?
>

To be honest this depends on how obj[propname] = aValue is used.  Is 
propname set to "author" from a static list of property names which are 
looped over?  If what is providing the propname value(s) isn't changing 
then no new instances of the string are created, the passed by reference.


> Thanks for any info!
>
> Angel "Java" Lopez
> @ajlopez
> github:ajlopez
>

Basically if the string "author" is cached in your code and the reference 
passed around new memory will not be allocated. The first time "author" has 
a value set, when it did not already exist in the object, will have memory 
allocated on that object for the property name to point to the value's 
reference.

Hope that helps

Jason 

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

Reply via email to