Do object references take up extra memory?

后端 未结 2 1895
Happy的楠姐
Happy的楠姐 2021-01-07 19:31

Lets say you have the following complex object:

var object1 = .... // (something complexed)

This takes up x amount of memory i

相关标签:
2条回答
  • 2021-01-07 20:01

    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.

    0 讨论(0)
  • 2021-01-07 20:01

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

    0 讨论(0)
提交回复
热议问题