question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Avoid using delete operator

See original GitHub issue

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):

_.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:closed
  • Created 9 years ago
  • Reactions:8
  • Comments:16 (5 by maintainers)

github_iconTop GitHub Comments

14reactions
justinmchasecommented, Mar 15, 2019

You can also delete with destructuring:

const obj1 = { a: 1, b: 2 }
const { a, ...obj2 } = obj1
console.log(obj2) // { b: 2 }
4reactions
robertrossmanncommented, Feb 28, 2015

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 ditching delete in favour of setting a property to undefined. 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 most delete calls are)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found