js-class-is icon indicating copy to clipboard operation
js-class-is copied to clipboard

Could symbol be added to the prototype instead of instace ?

Open Gozala opened this issue 4 years ago • 8 comments

Gozala avatar Jun 10 '20 23:06 Gozala

I think it can! However, while it will increase instances creation performance, it will decrease isX invocation performance, as it will have to fallback to the prototype.

Not sure what to do here.

satazor avatar Jun 11 '20 06:06 satazor

Sorry I submitted this in a rush, and forgot to provide more context. The reason this came up is I'm having to do this nasty bit of a hack:

https://github.com/ipfs/js-ipfs/pull/3081/files#diff-833dfdb70fcd89365dc3bddf93f78e7eR57

Which is needed because once CIDs cross thread boundaries their prototype chain is lost. Code under the link restores those prototype chains. However in case of CID turns out it is not enough because CID.isCID(v) is primarily how checks are performed, and it seems to return false even when v instanceof CID would return true, because that symbol field is set on instance rather than prototype. Which in turn is lost because symbols don't carry over during structure cloning.

When I started looking into this, I got wondering why that field is set on instance vs prototype and created this issue.

I'm not sure if moving symbol field is a best or adequate way to get rid of that nasty code I've linked to, but that was my immediate thought.

Gozala avatar Jun 11 '20 08:06 Gozala

@Gozala probably better to remove class-is and just use validateCID.

hugomrdias avatar Jun 11 '20 16:06 hugomrdias

@Gozala probably better to remove class-is and just use validateCID.

@hugomrdias remove from where ? All of the code base ?

Gozala avatar Jun 11 '20 17:06 Gozala

I was suggesting cid

hugomrdias avatar Jun 11 '20 21:06 hugomrdias

@hugomrdias sorry I'm not sure I understand what you mean by:

@Gozala probably better to remove class-is and just use validateCID.

Remove class-is from cids library ? If so do you mean also remove isCID static method or implementing it differently ? If former there's a lot of code that uses isCID and probably going to be an impractical to do that. If later would be good to know what you have in mind.

Gozala avatar Jun 11 '20 21:06 Gozala

I think it can! However, while it will increase instances creation performance, it will decrease isX invocation performance, as it will have to fallback to the prototype.

Not sure what to do here.

Another alternative could be to set it on both prototype and instance.

Gozala avatar Jun 11 '20 22:06 Gozala

Keep the api just without class-is, implement isCID with instanceof with fallback to validating the components

hugomrdias avatar Jun 12 '20 16:06 hugomrdias