-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Avoid using delete operator #375
Comments
Sounds like a reasonable request. Here's some benchmarks: http://jsperf.com/delete-vs-undefined-vs-null/16 Seems setting to undefined is the most ideal case if it's not too problematic to change. |
I am not against code optimisations, but I always believed there should be a balance between code readability and clarity and actual performance gains when deciding whether or not an optimisation should be done to code. In this particular case, we call That being said, |
Yep you make a fair point which is why I never second guessed using delete in the first place. That being said, if using it is relatively easy to switch, its always better to do that. Readability of |
@Alaneor I agree with you about this if you're thinking about premature optimizations, which in fact could be clearly considered as an anti-pattern, but this case is a bit particular and misunderstood. Let me explain that. The issue with Taking into account this package is commonly used in production by thousands of developers and companies, this kind of minor code optimizations is highly appreciated, and just because it cames from Google, and in general the people has a blind faith and expect the best quality from they (this is not a criticism, in fact it's the opposite. People love Google and it's an evidence). I've created the issue just after reviewing the code in a couple of modules, and noticing a bit of abuse of this operator. In general, most of the JavaScript hackers rarely use this operator, specially if his code is part of a package which is widely used by the community. |
Here are some articles that notice the same performance issue with a better technical explanation and endorsement: |
@h2non We always welcome pull requests, especially for small optimisations and fixes such as this. This would be a good first bug. |
Thanks @ryanseys. Sure! probably I'll send a PR with some improves when I've free time |
Before doing any sort of optimizations, we should profile the API and find out where the big perf hits exist. Blindly making a change like this could help, it could make things slower. Until we do an analysis and put a measurement framework in place, its impossible to know. I'm going to close this out for now. |
@brandondees @tmornini you guys are great at reminding me about premature optimizations. @JustinBeckwith raises a great point. You don't know what you don't know. 👍 |
@JustinBeckwith @snuggs If you can't measure it, you shouldn't optimize it. So, if you really want to optimize your code, measure everything. 😄 |
You can also delete with destructuring: const obj1 = { a: 1, b: 2 }
const { a, ...obj2 } = obj1
console.log(obj2) // { b: 2 } |
@justinmchase this is inefficient as it creates an additional object (and potentially wakes up the garbage collector). |
@snuggs how so? |
@ug02fast Hope this helps. |
@ug02fast as an addendum come to think of it there are TWO unnecessary objects. |
The use of the
delete
operator has performance negative effects for the V8 hidden classes pattern.In general it's recommended do not use it.
Alternatively, to remove object own enumerable properties, we could create a new object copy without those properties (example using lodash):
Or even define the property value to
null
orundefined
(which is implicitly ignored when serializing to JSON):The text was updated successfully, but these errors were encountered: