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? |
|||||
|
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. 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. EDIT 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 2nd 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). |
|||||||||||||||||||||
|
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
|
|||||||||||||||||||||
|
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. |
|||||||||||||
|
performance test http://jsperf.com/array-clear-methods/3
|
|||||||||||||
|
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. |
|||
|
How about the below modified version of Jan's initial suggestion?
|
|||||
|
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 |
|||||||||||||
|
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. |
|||||||||||||||||
|
I just did this on some code I am working on. It cleared the array. |
||||
|
behavior comparison between arr = [] vs arr.pop() loop:
|
|||
|
Simply re initialize it. No need to delete even.
|
|||||
|
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 answers, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?