Avoid using delete operator
See original GitHub issueThe 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):
_.omit(o, 'prop', 'prop2')
Or even define the property value to null
or undefined
(which is implicitly ignored when serializing to JSON):
o.prop = undefined
Issue Analytics
- State:
- Created 9 years ago
- Reactions:8
- Comments:16 (5 by maintainers)
Top Results From Across the Web
5 things you need to know about the delete operator in ...
The delete operator removes a property from an object. It cannot delete a variable. Any property declared with var cannot be deleted from...
Read more >delete operator - JavaScript - MDN Web Docs - Mozilla
The delete operator removes a property from an object. If the property's value is an object and there are no more references to...
Read more >Is using the Js delete operator to remove a property from ...
No, it's not considered bad practice. It just depends on what you want to do,. Read this thread: Garbage Collection and JavaScript "delete": ......
Read more >Avoid the “delete” Keyword in JavaScript | by Dhairya Shah
Let me explain, You should not use delete to remove the property from the object because this mutates the original and that can...
Read more >JavaScript delete Operator
You should never use the JavaScript delete operator on inbuilt objects such as window, Math, and Date. Using delete on any of these...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
Top GitHub Comments
You can also delete with destructuring:
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
delete
about 5 times (did not count properly) before each API request which usually takes about 200-600ms to complete. I am skeptic on the performance boost gained by ditchingdelete
in favour of setting a property toundefined
. In practice, we are probably talking about gains of 1ms/request at most.😃That being said,
lib/apirequest.js
sure could use some rewrite! (that’s where mostdelete
calls are)