Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

sukima
Copy link

@sukima sukima commented Dec 16, 2020

Allows to do delete this.myTrackedObject.foo or Object.deleteProperty(this.myTrackedObject, 'foo').

@sukima sukima force-pushed the patch-1 branch 3 times, most recently from 76614fd to c392331 Compare December 16, 2020 16:19
Allows to do `delete this.myTrackedObject.foo` or `Object.deleteProperty(this.myTrackedObject, 'foo')`.
@pzuraq
Copy link
Collaborator

pzuraq commented Dec 16, 2020

This seems good! Will merge once tests are passing, thanks for taking this on!

It's worth noting that delete is a pretty expensive operation usually, so it's something you may want to avoid, but it's in line with the philosophy of tracked-built-ins to support all possible operations transparently

@gnclmorais
Copy link
Contributor

It's worth noting that delete is a pretty expensive operation usually (…)

@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 gnclmorais mentioned this pull request Dec 19, 2020
@gnclmorais
Copy link
Contributor

@sukima, the build is now fixed, so feel free to rebase master onto your branch to get this going. 😃

@pzuraq
Copy link
Collaborator

pzuraq commented Dec 19, 2020

@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 Object.create(null), which I puts the object directly into dictionary mode.

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.

@boris-petrov
Copy link

This PR fixes #27 and is doing the same thing as #30.

@gnclmorais
Copy link
Contributor

@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) {
Copy link

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.

Copy link
Contributor

@gnclmorais gnclmorais Mar 23, 2021

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.

Copy link

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

Copy link
Contributor

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.

@chriskrycho chriskrycho added the enhancement New feature or request label Apr 22, 2021
@chriskrycho
Copy link
Collaborator

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.

@chriskrycho
Copy link
Collaborator

Closing this in favor of the slightly cleaner implementation in #30. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants