Does deletion and subsequently .push () use an array performance value / memory consumption?

Problem

Using delete an array element to remove it from the array is the only way I know to remove the element from the array so that the call to .forEach() skips the index.

Questions

  • Does delete the exampleArray[i] index, for example, to call the subsequent exampleArray.push() to increase the memory consumption of an array object?
  • How does deleting an object affect the garbage collector?

  • Is there a more efficient way to get rid of the exampleArray Element?

First example

 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, thanks.

+6
source share
2 answers

Does the delete use delete in the exampleArray[i] index, for example, for the subsequent exampleArray.push() to increase the memory consumption of the array object?

push increases memory consumption regardless of whether delete precedes it. Usually. If the engine has a pre-allocated storage for additional items, this may not be the case. If you think that the engine can somehow reuse the space open by delete to avoid allocating additional memory for the next push , most likely it will not.

How does deleting an item affect the garbage collector?

A deleted item obeys the GC if it does not remain in scope.

Is there a more efficient way to get rid of an exampleArray element of an element?

You have to decide whether you decide or not to end up with a sparse array. If you do not, and as you specify forEach , etc. Skip the holes, then delete will be faster. If you want to compress the array around the element to be deleted, for example, splice , the cost is likely to be 2 times higher.

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

The key point in GC is that you do not need to worry about it. You do not want to invade the engine. You can optimize one engine and find that performance on another engine has deteriorated.

All these micro-optimization issues are relevant only when you have logic that includes huge data objects on which you execute millions of statements. If so, you might want to flip your own data structure.

+7
source

delete There is a very specific function in javascript: removing a property from an object . You should not try to use it to remove elements from an array.

Use Array.prototype.splice instead :

eg:

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

In response to a garbage collection question, the GC is not affected by delete calls unless they delete a single reference to an object. delete does not force GC or does not make it easier.

0
source

All Articles