Is there a way to empty an array and if so possibly with .remove()
?
For instance,
A = [1,2,3,4];
How can I empty that?
Is there a way to empty an array and if so possibly with For instance,
How can I empty that? |
|||||||||
|
Ways to clear an existing array Method 1 (this was my original answer to the question)
This code will set the variable This is also the fastest solution. This code sample shows the issue you can encounter when using this method:
Method 2 (as suggested by Matthew Crumley)
This will clear the existing array by setting its length to 0. Some have argued that this may not work in all implementations of JavaScript, but it turns out that this is not the case. It also works when using "strict mode" in ECMAScript 5 because the length property of an array is a read/write property. Method 3 (as suggested by Anthony)
Using Method 4 (as suggested by tanguy_k)
This solution is not very succinct, and it is also the slowest solution, contrary to earlier benchmarks referenced in the original answer. Performance Of all the methods of clearing an existing array, methods 2 and 3 are very similar in performance and are a lot faster than method 4. See this benchmark. As pointed out by Diadistis in their answer below, the original benchmarks that were used to determine the performance of the four methods described above were flawed. The original benchmark reused the cleared array so the second iteration was clearing an array that was already empty. The following benchmark fixes this flaw: http://jsperf.com/array-destroy/151. It clearly shows that methods #2 (length property) and #3 (splice) are the fastest (not counting method #1 which doesn't change the original array). This has been a hot topic and the cause of a lot of controversy. There are actually many correct answers and because this answer has been marked as the accepted answer for a very long time, I will include all of the methods here. If you vote for this answer, please upvote the other answers that I have referenced as well. |
|||||||||||||||||||||
|
If you need to keep the original array because you have other references to it that should be updated too, you can clear it without creating a new array by setting its length to zero:
|
|||||||||||||||||||||
|
FYI Map defines Or as an Underscore.js mixin:
FYI it cannot be simplified to And the tests that goes with it:
Here the updated jsPerf: |
|||||||||||||||||||||
|
A more cross-browser friendly and more optimal solution will be to use the
|
|||||||||||||||||||||
|
Performance test: http://jsperf.com/array-clear-methods/3
|
|||||||||||||||||||||
|
You can add this to your JavaScript file to allow your arrays to be "cleared":
Then you can use it like this:
Or if you want to be sure you don't destroy something:
Lots of people think you shouldn't modify native objects (like Array), and I'm inclined to agree. Please use caution in deciding how to handle this. |
|||||||||||||
|
This is interesting. The answers that took no less that 1808 up-votes by now are misleading and incorrect. The question is "how do you empty your existing Array object"? E.g.:
Because they are two separate; individual objects, with their own two identities taking their own physical space of their digital world, each on its own. Let's say your mother asks you to empty the trash can.
- You don't bring in a new one as if you've done what you've been asked for.
Instead, you empty the trash can.
You don't replace the filled one with a new empty can, and you don't take the label "A" from the filled can and stick it to the new one as in Emptying an array object is the easiest thing ever:
This way, the can under "A" is not only empty, but also as clean as new!
No, you were asked to empty it.:
This is the only equivalent syntax of the correct sense of "emptying" / "throwing away" the content of the given JavaScript Array Object. |
|||||||||||||||||||||
|
and call it: =) |
|||||||||||||||||||||
|
In case you are interested in the memory allocation, you may compare each approach using something like this jsfiddle in conjunction with chrome dev tools' timeline tab. You will want to use the trash bin icon at the bottom to force a garbage collection after 'clearing' the array. This should give you a more definite answer for the browser of your choice. A lot of answers here are old and I wouldn't rely on them but rather test as in @tanguy_k's answer above. (for an intro to the aforementioned tab you can check out here) Stackoverflow forces me to copy the jsfiddle so here it is:
And you should take note that it may depend on the type of the array elements, as javascript manages strings differently than other primitive types, not to mention arrays of objects. The type may affect what happens. |
|||
|
There is a lot of confusion and misinformation regarding the while;pop/shift performance both in answers and comments. The while/pop solution has (as expected) the worst performance. What's actually happening is that setup runs only once for each sample that runs the snippet in a loop. eg:
I have created a new test that works correctly : http://jsperf.com/empty-javascript-array-redux Warning: even in this version of the test you can't actually see the real difference because cloning the array takes up most of the test time. It still shows that |
|||||||||||||
|
Use a modified version of Jan's initial suggestion:
|
|||||
|
Simply to empty array
or
|
|||
|
I just did this on some code I am working on. It cleared the array. |
|||||
|
If you are using
or
then you are just creating new array which points to specific memory location. This means previous array will be remain in memory till garbage collection. So its not the better way to use. Instead of these two solutions are better.
and
As per previous answer by kenshou.html, second method is faster. |
|||||||||||||||||||||
|
To Empty a Current memory location of an array use:
Example:
Use Empty Array with new Memory address use : slice(0, 0)
To assigning new empty array
|
|||||
|
You can use:
|
||||
|
Behavior comparison between
|
||||
|
By using loop process to empty an array in java script.
|
|||
|
There are different ways to clear array depending on requirement. The results below are for chrome.
array = []; does not clear original array shared among other references, it only creates new instance. a.splice(0,a.length); clears memory also. |
|||
|
The fastest solution for all current browsers is to implement the
Or you could just use a while loop:
But the best answer as found above would be to use the
|
|||||||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?