1

I've created an A object using the following code:

function A(){
    this.one = "one"
    this.two = "two"
    this[0] = "numeric index"
}
var objs = [];
for (var i=0; i<10;i++){
    // create 10 objects to get object size without slack/space for additional properties
    objs.push(new A())
}
objs.forEach(function(obj){
    obj.extraProperty = "extra"
})

As expected, the DevTools heap snapshot shows the size as 40 bytes:

  • 2*8 bytes for the two properties one and two
  • 8 bytes for extra properties
  • 8 bytes for elements (numbered properties)
  • 8 bytes for the hidden class

Based on this explanation of V8 object representation.

However, the heap snapshot also lists __proto__ as a property, but I assume it doesn't add anything to the size of the object.

It is also already accessible as part of the hidden class/Map, so storing an additional reference to it isn't necessary.

Is the __proto__ property just shown for convenience? How is it intended to be used?

1 Answer 1

2

It has the same meaning as it already stands for. Instances have __proto__, classes have prototype.

function User(name){
    this.name = name
 }; 

var vinoth = new User("v");

vinoth.__proto__ == User.prototype //true

We have new User(), then vinoth instanceof User is true. __proto__ look up helps up in looking up the Prototype Hierarchy which would point to User.prototype which would further point to Object, because everything is an Object instance. Further Heirarchy lead ends up to null.

__proto__ is just a reference which chrome dev tool provides.

9
  • 1
    Yes, but why does it show up in the heap snapshot? It's not stored directly on the object, but on Object.prototype. Feb 14, 2017 at 10:53
  • Look at my updated answer.... basically its just a reference which chrome dev tool provides... ultimately everything is an Object instance... so you have properties which can be directly accessed in javascript...
    – Thalaivar
    Feb 14, 2017 at 11:33
  • Yes, but why does it show up in the heap snapshot if it's not an own property of the object, unlike the other properties? Feb 14, 2017 at 14:30
  • 1
    @MattZeunert system / * stuff is only visible if you explicitly enable it - __proto__ is there to make this connection between object and its prototype visible even if system properties themselves are hidden. Otherwise people would be extremely confused why object retains its prototype. Feb 14, 2017 at 15:25
  • 1
    @MattZeunert there used to be, now that I look at Settings page, I don't see it anymore. It was called "Show advanced heap snapshot properties". Maybe it got defaulted to true at some point because everybody was enabling it anyway... Which makes showing __proto__ obsolete Feb 15, 2017 at 16:11

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.