delete an attribute of an object more optimally

9

I have an empty object defined (associative array)

obj = {}

If I try to consult some parameter this will return undefined to me because it is empty.

>> console.log(obj.id)
undefined

now I assign a value to id

>> obj.id = 123
>> console.log(obj.id)
123

Suppose I want to delete the id attribute which of these 2 is more convenient

obj.id = undefined

or

delete obj.id

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

For the first case:

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

On the other hand, for the second case:

>> console.log(obj)
{}

Which is the best, there is another alternative.

    
asked by Ricardo D. Quiroga 03.08.2017 в 16:47
source

3 answers

5

In this world of programming is not that one thing is better than another if not the one that best suits your needs

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

Putting 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 in a defined 296,204,746 objects and only 10,851,554 with delete (approx)

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

    
answered by 03.08.2017 / 17:40
source
4

To delete an object from javascript you must use the word delete

let obj = {
  id : 2 , 
  nombre : "el nombre"
    };
obj.nombre = undefined;
delete obj.id;
console.log(obj.id !== "undefined")// no declarado
console.log(obj.nombre !== "undefined")//  declarado sin valor
//para ver si existe en el objeto
console.log('id' in obj); 
console.log('nombre' in obj); 
console.log(obj);

Perform the command obj.id = undefined is to initialize the variable. If you want to send the JSON without the value id you must use delete , otherwise if you want to fill a form with initial values, you are required to send the value to your Backend you must declare the variables as you show In the example it is visualized that of the two forms it is undefined but when you consult if the element exists in the object it exists and it prints it to you

    
answered by 03.08.2017 в 16:59
3

To delete a property from an object in JavaScript use the operator delete since assigning the value undefined does not erase it.

The advantage is that delete deletes the property and therefore memory is freed and when traversing the properties of the object it becomes faster.

Reference

answered by 03.08.2017 в 17:17