miktam / sizeof

Get size of a JavaScript object
MIT License
311 stars 45 forks source link

About Circular structure in Node #38

Closed halilcakar closed 4 years ago

halilcakar commented 5 years ago

Hey @miktam, First of all thanks. Let view the situation of a Circular object in node.

In my code I have a USERS object that holds my users personal data's (obvv :) and i was playing around and found out that if I reference my object as Circular object (USERS[3].user = USERS[3];) it gives me this value: 11076980 bytes = ~10.56 MB but when i remove my reference(or not create in this matter) it gives me: 1940 bytes = ~0.0018MB which is quite a difference 😄

I saw that u have this: https://github.com/miktam/sizeof/blob/81557e3c115bdbf049876c2b5e484e5ca820cedf/index.js#L20-L29

But it really doesn't detect Circular objects. What do you think about this ?

miktam commented 5 years ago

@halilcakar right, correct way to implement circular dependencies would be to hold a structure (eg Set) of all the objects, and do not count duplicates. let me think about this a bit more and will get back to you.

halilcakar commented 5 years ago

That's actually not a problem for my case, but when i saw i just wanted to write about it cause it might me someone else's case. I'm also gonna look into this, let you know if i would find some solution about it :)

stpnov commented 4 years ago

I have the same problem. Infinitely considers

jamesacres commented 4 years ago

Doing the following means sizeof(object[key]) is never called as it only continues if object doesn't have key as a property.

if (!Object.hasOwnProperty.call(object, key)) {
  continue
}

Instead can check if it references itself like if (object[key] === object) or create a WeakSet to not count duplicates as suggested above.