6

Problem

Using delete on an array element to remove it from the array is the only way I'm aware of to remove an element from an array such that a .forEach() call skips the index.

Questions

  • Does using the delete on an index, exampleArray[i] for example, cause a subsequent exampleArray.push() to increase the memory consumption of the array object?
  • How does deleteing an object effect the garbage collector?

  • Is there a more efficient way of ridding an exampleArray of an element?

Example of the former

var exampleArray = [];
var n = 500;

//Does this line imply a memory allocation?
exampleArray.length = n;

exampleArray.fill("Lorem Ipsum", 0);

exampleArray.forEach(function(cur, ind, arr) {
  if(ind % 4 === 0) {
    delete arr[ind]; //Actually deletes the object itself, index no longer exists
    //Length does not change, however. Does available memory?
  }
}, this);

n /= 4;

//Where, in memory, are these placed?
while(n--) exampleArray.push("amet dolor");

Any help is appreciated, thank you.

6
  • 3
    Your assumptions are all pretty much wrong. delete is not the only way to remove an index from an array, in fact, it doesn't do that at all, and isn't even intended for arrays ? How it affects the garbage collector, and anything else, really becomes irrelevant after that ?
    – adeneo
    Dec 23, 2015 at 3:48
  • I haven't made any assumptions, though it sounds like you have. I said it was the only way I was aware of, care to enlighten me? Dec 23, 2015 at 3:49
  • 1
    @AndrueAnderson if you're looking for short and sweet answers to your questions, they are 1) no 2) it doesn't 3) [].splice
    – Andbdrew
    Dec 23, 2015 at 3:56
  • I appreciate your confidence, I'm looking for tests. Thanks. Do you think I'd have posted it here if page on MDN provided the answer @adeneo ? Dec 23, 2015 at 3:57
  • tests? the documentation covers all three answers pretty handily. See the links in my answer below.
    – Andbdrew
    Dec 23, 2015 at 3:58

2 Answers 2

7

Does using the delete on an index, exampleArray[i] for example, cause a subsequent exampleArray.push() to increase the memory consumption of the array object?

push increases memory consumption whether or not it was preceded by a delete. Usually. If the engine has pre-allocated storage for additional items, perhaps it would not. If you are imagining that the engine might somehow be able to re-use the space opened by the delete in order to avoid allocating additional memory on the next push, most likely not.

How does deleting an element effect the garbage collector?

The deleted element is subject to GC if it does not otherwise remain in scope.

Is there a more efficient way of ridding an exampleArray of an element?

You have to decide whether or not you mind ending up with a sparse array. If you do not, and as you point out forEach etc. skip over the holes, then delete is fastest. If you want to compress the array around the deleted element, using splice for example, the cost is likely to be 2x higher.

Engines implement different strategies for representing arrays internally, and sometimes switch between them--for instance, when an array reaches a particular degree of sparseness. Each engine will have a different strategy. The only reliable way to answer these kinds of performance questions is to run performance tests, or read the engine source.

They key point about GC is that you don't have to worry about it. You don't want to get into second-guessing the engine. You could optimize for one engine, and find that performance on another engine got worse.

All these micro-optimization questions are relevant only when you have logic involving huge data objects on which you are performing millions of operators. If that is the case, it may be the case that you want to roll your own data structure.

1
  • THANK you for actually reading the question, and noticing the context of .forEach() which deal with sparse arrays specifcially. I wish I could edit my post to specify WHOSE time I appreciate. Dec 23, 2015 at 3:59
0

delete in javascript has a very specific function: removing a property from an object. You should not attempt to use it to remove items from an array.

Instead, use Array.prototype.splice:

for example:

var arr = [1,2,3,4];
arr.splice(1, 1);

console.log(arr); // [ 1, 3, 4 ]

In answer to the question about garbage collection, GC is unaffected by calls to delete unless they happen to remove the only reference to some object. delete does not force GC or facilitate it.

2
  • I'm not the downvoter, but I would note that an array element is a property on an object. There are certainly use cases for using delete, as long as you do not mind ending up with a sparse array.
    – user663031
    Dec 23, 2015 at 4:01
  • @torazaburo I think in this context the index is the property, not the actual element at the index
    – Andbdrew
    Dec 23, 2015 at 4:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.