2

Lets say I have created an object and its properties in JavaScript as follows-

   var obj = {};
   obj['bar'] = 123;
   obj['bar']['foo'] = new Array();
   obj['bar']['xyz'] = new Array();

After this, I push elements into the two arrays. If I then write

   delete obj['bar'];

Will the two arrays also be deleted ?

1
  • 2
    Deleted in the sense that the delete operation was successful, or deleted in the sense that they're GC'd? Commented Jul 4, 2012 at 16:38

3 Answers 3

2

Will the two arrays also be deleted ?

They'll be eligible for garbage collection, assuming nothing else has any references to them. And nothing will, based on that code. When and whether they're actually cleaned up is up to the implementation.

But note that they're eligible for GC even before you remove bar, because your code is doing something quite odd. See the comments:

// Creates a blank object, so far so good.
var obj = {};

// Sets the `bar` property to the number 123.
obj['bar'] = 123;

// Retrieves the value of `bar` (the primitive number 123) and
// *converts* it into a `Number` instance (object), then
// creates a property on that object called `foo`, assigning a
// blank array to it. Because the Number object is never stored
// anywhere, both it and the array are IMMEDIATELY eligible for
// garbage collection. The value of `obj['foo']` is unaffected,
// it remains a primitive number.
obj['bar']['foo'] = new Array();

// The same happens again, a temporary Number instance and
// array are created, but both are IMMEDIATELY eligible for
// garbage collection; the value of `obj['bar']` is unaffected.
obj['bar']['xyz'] = new Array();

So you didn't even have to remove bar, the arrays were instantly eligible for garbage collection. This happens because in JavaScript, numbers are primitives that can be automatically promoted to Number objects — but that doesn't affect the value of the property the primitive number is assigned to. So:

var obj = {};
obj['bar'] = 123;
obj['bar']['foo'] = [];         // [] is a better way to write new Array()
console.log(obj['bar']['foo']); // "undefined"

If you change your obj['bar'] = line to:

obj['bar'] = {};

or

obj['bar'] = []; // Arrays are objects

...then the foo and xyz properties will not instantly disappear, and the arrays will stick around until bar is cleaned up.

Sign up to request clarification or add additional context in comments.

Comments

2

delete itself won't clear arrays, but will remove the only existing reference to them. Unless you capture reference to them somewhere else, yes, they will be claimed by GC as they would have no references after that.

When, how, or whether their memory is actually reclaimed by GC is implementation dependent.

4 Comments

OK. So it would be same as if i issued delete['obj'][arrayname] individually ?
No. You'd still have an empty object after that. But arrays would be gone (once again, unless referenced somewhere else).
Actually i wanted that they be GC'd and their space be freed. That will be accomplished right ?
They'll become eligible. Updated answer.
1

Yes you can delete the obj['bar'] property which will make the associated arrays in obj['bar'] eligible for garbage collection (assuming there are no other references to them elsewhere).

You can not delete obj. You could however use obj = null; to free obj's properties for potential garbage collection.

The delete function returns true if it has successfully deleted something as you can see in this jsfiddle

alert(delete obj['bar']); // alerts true;

You can not delete vars or functions however.

var x = 2;
alert(delete x); // alerts false;

function x(){};
alert(delete x); // alerts false;

For further reading here's an amazing article on javascript's delete function: http://perfectionkills.com/understanding-delete/

1 Comment

Such a wall of text to cover "delete only deletes object properties"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.