-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add deleteProperty to TrackedObject #35
Conversation
76614fd
to
c392331
Compare
Allows to do `delete this.myTrackedObject.foo` or `Object.deleteProperty(this.myTrackedObject, 'foo')`.
This seems good! Will merge once tests are passing, thanks for taking this on! It's worth noting that |
@pzuraq, is there a lighter alternative when you want to remove a key from an onbject? For example, is creating a new object with all the original keys minus the one we want to remove “cheaper”? |
@gnclmorais generally it's a bad idea to change the shape of an object, either by adding or removing fields, because the engine will optimize access to the fields based on the shape of the object. Deleting a key both changes the shape, and I believe transitions the object into dictionary-mode, which is even worse performance because then it essentially doesn't have a shape at all, and every access is less optimal. If you are adding/removing keys regularly, I tend to recommend using a Map instead of an object, because they are built for that. Another option is to do I also generally strictly separate my dictionaries/maps, which have mutable shapes, from classes or object literals, which I ensure always have the same shape. |
@pzuraq, thank you for your answer, quite insightful and gave me some food for thought! |
@@ -50,6 +50,17 @@ const proxyHandler = { | |||
return true; | |||
}, | |||
|
|||
deleteProperty(target, prop, receiver) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, according to the spec, deleteProperty
has no receiver
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True if you look at an Object’s deleteProperty
, but in this case, we’re dealing with a Proxy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm sorry, but I don't really understand. What is "Object's deleteProperty
"?
I meant proxyHandler.deleteProperty has no receiver
argument, tc39/ecma262#1198
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you’re right, @nag5000. 👍 I got confused.
Happy to land this (presumably as a minor release after the major I'm releasing today). This could use a rebase and for the last couple comments to be addressed, however. |
Closing this in favor of the slightly cleaner implementation in #30. Thanks! |
Allows to do
delete this.myTrackedObject.foo
orObject.deleteProperty(this.myTrackedObject, 'foo')
.