javascript – delete an attribute from an object


I have defined an empty object (associative array)

obj = {}

If I try to consult any parameter, it will return undefined when it is empty.

>> console.log(

now I assign a value to id

>> = 123
>> console.log(

suppose I want to delete the id attribute which of these 2 is more convenient = undefined



When I check the independent attribute of which I have used it will return undefined but if I do console.log() of only the object I have something different:

For the first case:

>> console.log(obj)
{id: undefined}

Instead for the second case:

>> console.log(obj)

Which is the best, there is another alternative.


In this world of programming, it is not that one thing is better than another, but the one that best suits your needs.

Using delete is sooo slow and it can affect the performance of your application if you delete thousands and thousands of properties, but are you really going to delete so many properties? If you delete some, your application will probably slow down about 200 milliseconds, which nobody will notice, however if you are going to save your objects in a non-rational database or you simply have to send objects to the server and users have slow connections, that is better to waste a few milliseconds deleting properties; I have worked with satellite networks and it is a headache to send large things by request.

Setting it to undefined is faster but you will have a lot of garbage dragging, that if it does not affect you there will be no problem.

I leave you a benchmark where you can see that in a second they put 296,204,746 objects in a defined and only 10,851,554 with delete (approx)

So use the one that best suits you or with which you can have a cleaner and more readable code so that maintenance is more efficient.

Scroll to Top