Do object references take up extra memory? Do object references take up extra memory? javascript javascript

Do object references take up extra memory?


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....