Lets say you have the following complex object:
var object1 = .... // (something complexed)
This takes up x
amount of memory in your JS application. Now lets say you have some other objects that reference object1
:
var otherObject = { something: true, value: 'yes', object: object1 }; var anotherObject = { color: '#FFF', object: object1 };
Have I tripled the amount of memory that object1
originally took up? Or do the references to object1
not add to the overhead of the memory used?
I'm not sure how to test this myself in order to determine the answer. (Bonus points if you can tell me how to point me to a tool that helps benchmark this).
Example# A reference is not an object, and unlike an object, it is not guaranteed to occupy some contiguous bytes of memory.
To answer your question in a more general way: the C++ standard does not require that references should occupy memory, but it does not require that they should not. The C++ compiler is free to compile the code in any way, as long as the results are correct, and what they are expected to be.
A Formula that references a field on another object is known as a Spanning Relationship. The limit of spanning relationships per object is 15. This means that an object can only contain up to 15 different object references.
In contrast, JavaScript automatically allocates memory when objects are created and frees it when they are not used anymore (garbage collection). This automaticity is a potential source of confusion: it can give developers the false impression that they don't need to worry about memory management.
Objects are always passed by reference in JavaScript (see this popular answer). Pointer to an object takes some amount of memory (depends on implementation), of course, but much less than the actual object.
Take a look at this question. Numbers, strings, etc. are always called by value, but objects are called by sharing; that is they are called by value, but the value is a reference to the object.
In other words, if you modify the pointer's properties, you are modifying the same pool of memory as the object. But if you reassign the pointer, it does not affect the original object.
What this means is that, in your example, you have not tripled the amount of memory that object1
took up, but the extra pointers to object1
will take up some memory space. Exactly how much space? That depends on the precise implementation of the Javascript engine, but it will always be much less than the size of the object.
As far as benchmarking goes, look at Mozilla's documentation for their JS engine, SpiderMonkey. There are lots of good utilities there....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With