4

I just found out that javascript has a delete statement. I've read a bit about it and am not much the wiser.

So I am hoping to get a functional definition of when I should use it, if at all. So I know I can delete properties of an object; as is made obvious by this fiddle:

var myData = {a:"hello",b:"world"};
alert(myData.b);
delete myData.b;
alert(myData.b);

Which shows "world" then undefined in successive alerts. However, you cannot use delete like this (as one might in C++):

function data() {
    this.attribute1 = "aww";
    this.attribute2 = "poo";
}

var myData = new data();
delete myData;

Here delete returns false indicating that you cannot delete myData. I used to work primarily in C++ and this was like the whole idea of delete. I can't think of any reason I would use delete to remove properties. Should I ever worry about using delete to mark memory to be freed? Like if I do something like this.

var myData = new data();
... //do stuff
myData = new data();

Addition

So I dug up the post that confused me. The most upvoted answer on this question states (as quoted from the Apple Javascript Coding Guidelines):

Use delete statements. Whenever you create an object using a new statement, pair it with a delete statement. This ensures that all of the memory associated with the object, including its property name, is available for garbage collection. The delete statement is discussed more in “Freeing Objects.”

So, if I understand some of the comments and answers I've been given, this statement is not accurate, because you cannot even call delete on an object created using a new statement.

Community
  • 1
  • 1
Ian
  • 4,169
  • 3
  • 37
  • 62
  • if you want to 'delete' an entire object then set it to null i.e. myData = null; –  Nov 11 '15 at 20:56
  • (As long as nothing else has a reference to the same data.) – Dave Newton Nov 11 '15 at 20:57
  • Here's a really good treatise on `delete` in JS: http://perfectionkills.com/understanding-delete/ – Rick Viscomi Nov 11 '15 at 21:03
  • @jeff - So I've got a few large array variables that I reset at different points in my code without worrying about the data that used to be pointed to. Should I explicitly set those variables to null before resetting them to the new data? Like so: myData = ["a","b"]; myData = null; myData = ["c","d"]; ? – Ian Nov 11 '15 at 21:04
  • @Ian: That question was posted over 6 years ago. JavaScript has changed since then, and what is may not necessarily be what was. It very could have been accurate in 2009 - I can't speak on that as I wasn't even in college at that point. – Bardicer Nov 11 '15 at 21:06
  • @Ian No. If something else has a reference to those arrays it wouldn't matter anyway, and if you set the variable to new data with nothing referencing the original, it's eligible for GC anyway. It's rare you want to mess with internal memory management. – Dave Newton Nov 11 '15 at 21:06
  • @Dave Newton - Alright, that's the main use case I was worried about. So I think the message is basically that I don't have to worry about it, even when reallocating very large data arrays. – Ian Nov 11 '15 at 21:10

2 Answers2

4

According to mozilla's developer documents, delete does not work that way.

The delete operator deletes a property from an object, it does not delete the object itself.

So instead of using it as you have demonstrated, you would use it more like the following:

myGlobalObject = {};
var myObject = {};
myObject.propertyA = "blah";

// Do some stuff

delete myObject.propertyA; // This works because you're deleting a property off myObject
delete myGlobalObject; // This works because myGlobalObject is a property of the global object.
delete myObject; // This does NOT work - most likely because you declared it using the var keyword

This doesn't actually do garbage collection though. Also if myObject has a prototype up the chain that has propertyA it would still inherit that property through the prototype.

For more indepth information feel free to check out the developer documents:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

Bardicer
  • 1,405
  • 4
  • 26
  • 43
  • The OP's first example shows he's already aware of the property removal aspect of `delete`, no? It *does* break references, which *may* enable some memory to be freed. – Dave Newton Nov 11 '15 at 20:55
  • Both the question and answer have been edited. And breaking references doesn't seem like an ideal way to do garbage collecting. Basically the equivalent of throwing your trash on the street somewhere and hoping someone picks it up before it causes any damage. – Bardicer Nov 11 '15 at 21:01
  • 1
    I think you're misunderstanding what "breaking references" means; deleting doesn't *directly* free memory, but once the *reference* is gone ("broken reference") the associated memory is *eligible* for GC. (If nothing else references the same data, anyway.) – Dave Newton Nov 11 '15 at 21:05
  • 1
    (For that matter, setting a value to `null` doesn't directly free memory either, just like in most memory-managed languages.) – Dave Newton Nov 11 '15 at 21:18
2

delete on its own specifically states:

The delete operator removes a property from an object.

You might remove a property if you don't want it included in data sent to a server, or used by other code, e.g., something that automatically takes object data and turns it into a table.

In general you'd never use it for memory management, a possible exception being if you had a huge chunk of data in an object (like received from the back end) that you explicitly don't need.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302