0

I have an array which has a single element which I delete with the following code:

delete array[0]

At this point the array is empty. I'm therefore confused why array.length has an output of 1.

When I use console.log it displays the following:

console.log

Am I doing something fundamentally wrong or how do I check that the array is empty? Maybe an explanation of typescript array memory management would be useful.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
John Wheal
  • 9,908
  • 6
  • 29
  • 39
  • Read the [Description for `delete` operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete#Description) which reads `... the delete operator has nothing to do with directly freeing memory`. So in your case, it deleted the value but not the memory used by the array. – Harsh Gupta Dec 30 '17 at 14:05
  • @jonrsharpe I have explained why this is not a duplicate in the question – John Wheal Dec 30 '17 at 14:08
  • 2
    Also it doesn't make sense on the face of it because *"TypeScript array memory management"* isn't a thing. TS compiles to JS, remember; there's no TypeScript at runtime. – jonrsharpe Dec 30 '17 at 14:23
  • While I agree your question is not an exact duplicate, the linked questions & answers do actually provide some interesting insight and it feels like they also answer your question. I tried to formulate something more specific, feel free to comment more if I am not understanding your needs correctly. – Pac0 Dec 30 '17 at 14:45
  • it's explained in the Deleting array elements section https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete#Deleting_array_elements – Slai Dec 30 '17 at 14:59

1 Answers1

1

TL;DR;

You should not use delete to remove an element of an array if you expect it to shrink.

Basically it will remove the reference for the first element, but won't shrink the array.

element [0] is empty -> array appears as [empty], because there is one empty spot in the array ! The size is still reserved in memory. And the length property is still 1.

So, this is not an empty array, it's a non-empty array with one empty spot

Other example :

var a = [3, 4];
delete a[0];

a in my JS console results as [empty, 4] .


Am I doing something fundamentally wrong or how do I check that the array is empty?

If you want to check if such an array is only containing emptys, you could could simply filter for undefined values :

var a = [3, 4];
delete a[0];
delete a[1];
if (a.filter(x => x !== undefined).length === 0) {
  console.log('array looks empty!');
} else {
  console.log('array doesn't look empty!');
}

(but of course this won't work if you have actual undefined values that have some meaning, I hope it is not the case)


Maybe an explanation of typescript array memory management would be useful.

It 's quite simple : there is no TypeScript memory management.

As already pointed out in comments, TypeScript is turned to Javascript, at runtime it is only JavaScript running, it's Javascript arrays and nothing more.

There is nothing special that Typescript is doing for you here. This will be transpiled verbatim to javascript.


For more info on the JS arrays and delete, see linked potential duplicate questions for details and explanations :

Pac0
  • 21,465
  • 8
  • 65
  • 74