18 Answers
18

This code will set the variable A to a new empty array. This is perfect if you don't have references to the original array A anywhere else because this actually creates a brand new (empty) array. You should be careful with this method because if you have referenced this array from another variable or property, the original array will remain unchanged. Only use this if you only reference the array by its original variable A.

This is also the fastest solution.

This code sample shows the issue you can encounter when using this method:

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.

Using .splice() will work perfectly, but since the .splice() function will return an array with all the removed items, it will actually return a copy of the original array. Benchmarks suggest that this has no effect on performance whatsoever.

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://jsben.ch/#/hyj65. 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.

> 0 is more readable IMHO. And there's no performance difference between the two.
– Philippe LeybaertAug 8 '14 at 19:46

10

@daghan, it's not at all clear what you're trying to say. b holds a reference to the old array even after a is assigned a new one. c and d continue to reference the same array. The difference in outputs is therefore expected.
– shovavnikAug 17 '14 at 8:03

6

@DiegoJancic Method #1 doesn't count because it doesn't clear the array. It creates a new one. It shouldn't be included in a benchmark.
– Philippe LeybaertNov 7 '14 at 14:15

42

You can't use while(A.pop()) in case an item in the array is falsey. Take for example A = [2, 1, 0, -1, -2] would result in A equaling [2, 1]. Even while(A.pop() !== undefined) doesn't work because you can have an array with undefined as one of the values. Probably why the compiler doesn't optimized it.
– Jonathan GawrychJan 9 '15 at 1:29

@Pacerier: It still works in ES5. From section 15.4: "...whenever the length property is changed, every property whose name is an array index whose value is not smaller than the new length is automatically deleted"
– Matthew CrumleyJun 21 '11 at 7:43

10

@LosManos Even in strict mode, length is a special property, but not read only, so it will still work.
– Matthew CrumleyJan 4 '13 at 14:18

10

@MattewCrumley I done some test, and it seems like a.length = 0 is not to efficient clearing whole array. jsperf.com/length-equal-0-or-new-array I think if you have one refence (and you haven't added extra properties that you want to keep), it is better to create new array, and leaves old to the garbage collector, that will run when appropriate.
– Paul BrewczynskiNov 16 '13 at 19:08

TT your answer is the only one that correct and fast ( at the same time ) but have some much less "upvotes". Well, it seems that people like pretty solutions that are slow :/
– obenjiroJun 26 '13 at 5:09

@naomik But this is one of the basic functionalities, which should have been there by default.
– thefourtheyeAug 19 '13 at 4:43

7

@thefourtheye Good solution for performance, though I agree with @naomik, you should not modify native objects. Saying that it should be there is beside the point, the problem is you're modifying globals, which is bad. If you're providing your code for others to use, then it should have no unforeseen side effects. Imagine if another library also modified the Array.prototype and it was doing something slightly different, then all throughout your code [].clear() was slightly wrong. This would not be fun to debug. So, the general message is: Don't modify globals.
– jpilloraSep 14 '13 at 10:39

3

@thefourtheye The whole point of not modifying global scope is because you won't know if someone else's code is already (or will be) using the name. I suggest a function inside local scope. So, inside your application's/library's IIFE, do function clear(arr) { while(arr.length) arr.pop(); }, then clear arrays with clear(arr) instead of arr.clear().
– jpilloraSep 15 '13 at 4:58

2

It turns out this method is a lot slower than .splice() and .length=0. The benchmarks were not correct. See my updated answer.
– Philippe LeybaertFeb 17 '15 at 1:30

We could prevent the resulting array from being returned by using the comma operator: A.splice(0, A.length),0;. This would leave a return value of 0 just as A.length = 0; would. The resulting array is still created and should cause the script to run slower: (jsperf ~56% slower). Browser implementation will affect this although I see no reason why splice would be faster than setting length.
– Evan KennedyAug 18 '13 at 3:47

7

I have also found that just A.splice(0) also works.
– corwin.amberDec 4 '14 at 17:49

This is because these two arrays are two separate, individual objects, with their own two identities, taking up their own space in the 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 A = [1,2,3,4]; A = [];

Emptying an array object is the easiest thing ever:

A.length = 0;

This way, the can under "A" is not only empty, but also as clean as new!

Furthermore, you are not required to remove the trash by hand until the can is empty! You were asked to empty the existing one, completely, in one turn, not to pick up the trash until the can gets empty, as in:

while(A.length > 0) {
A.pop();
}

Nor, to put your left hand at the bottom of the trash, holding it with your right at the top to be able to pull its content out as in:

A.splice(0, A.length);

No, you were asked to empty it:

A.length = 0;

This is the only code that correctly empties the contents of a given JavaScript array.

The only problem with your suggested solution is that the trash still exists, it is just that you changed the notice board saying that there is no trash. A reference to the old array should still exist. Can you be sure the garbage collector when setting .length = 0, will also remove all references to the array and its properties ? I think it does though, but to me that is magic. A .clear() method is desirable to avoid confusion to say the least.
– momomoAug 15 '16 at 9:52

4

I never stated that this solution is wrong. The problem is that this entire thread is completely unncessary. Over 3000 votes shows that trying to figure out what the best way is should make it a valid enough case for the EMCA crack head developers to add such a method. Nobody should have to go through figuring it out. There are three - four different ways to do it. In some benchmarks, the length solutions is much slower than others. Furthermore, the idea of setting .length = 0, for any reasonable developer would not be a satisfactory one.
– momomoAug 15 '16 at 14:40

2

Because to accomplish what it should, all references must be removed. .length = 0 is not even a method call, so if there is other actions taken when it is set to 0 ( which there is in most browsers through define setter ) I would still consider it too magical to actually trust it does what it supposed to do.
– momomoAug 15 '16 at 14:43

5

Therefore, I'd rather clear it myself. A clear method can be prototyped but that is just ugly. My point is that this implementation should already be present so that over 10 000 developers didn't have to spend hours reading just this thread, not considering all others who spent more time benchmarking.
– momomoAug 15 '16 at 14:47

1

there's only one way to empty your array fill it with new incoming data and discard it again. All others are either not, or are ridiculously inefficient and unforgivably cpu hungry. The only practical alternative is the A(n) = A.splice( 0, A.length ); in case you need to backup your previous content. p.s. Array.length is a read-write property \ method in case you've missed that basic fact. Meaning, you can expand it to a new length, you cant trim it to whichever length you'd want, and among others you can discard all members by shortening its length to 0. It's a Swiss-knife of the array.
– Bekim BacajAug 15 '16 at 18:34

Adding the percentage changes arent much use without also noting your platform. On my machine pop is only very mildly quicker in Chrome 34 but actually slower than [] in latest Firefox.
– Matt StylesMay 15 '14 at 10:56

@naomik Can you explain your reasoning why doing such a thing is frowned upon?
– UndefinedSep 16 '13 at 16:24

15

It is "frowned upon" to modify javascript primitive functions like Array and String. You could possibly be overloading an already existing function and trash the object class. There might be an obscure javascript engine that already has clear() and expects it to behave a different way. Tread carefully is all I say.
– Design by AdrianMar 25 '14 at 15:20

How about the problem where doing a foreach over the members of an array will suddenly start including a clear key?
– ErikEAug 14 '15 at 0:00

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:

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 splice is the fastest way to clear the array (not taking [] into consideration because while it is the fastest it's not actually clearing the existing array).

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.

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.

why do people have this tendency to grab the accepted answer and put it into a prototype function? Do you actually do this in your projects? Do you have a huge library of prototype additions that you include in every project?
– nurettinNov 29 '13 at 11:05

@naomik "Please don't encourage modification of the native objects." -- I completely agree with this, but just repeating the sentence by itself comes across as arrogant. Someone proposing such a solution is likely not aware of the consequences, and dropping this line on them instead of providing a short explanation or a link does not convey a meaning other than "we, people smarter than you, tell you not to do this, because we know better".
– John WeiszDec 11 '16 at 11:20

Then you are assigning new array reference to a, if reference in a is already assigned to any other variable, then it will not empty that array too and hence garbage collector will not collect that memory.

Apart from being wrong on a.length, I don't see what this new answer adds to the thread?
– BergiDec 1 '14 at 14:33

@Bergi I just want to focus about actual memory representation about array
– Laxmikant DangeDec 2 '14 at 9:30

Do you have any source to confirm which JS engines will create a new array when a.length=0; is performed? How would those engines act for a.length=500; and a.length=4;?
– joeytwiddleDec 13 '14 at 18:38

I tried it on most of browsers, like IE, Firefox, Chrome, it is creating new array. If you set length greater than 0 then it will create an array with undefined elements, i.e. it will just hold some memory locations.
– Laxmikant DangeDec 16 '14 at 15:21

There are also tricky way which you can think about, for example something like this:

arr.splice(0, arr.length); //[]

So if arr has 5 items, it will splice 5 items from 0, which means nothing will remain in the array.

Also other ways like simply reassign the array for example:

arr = []; //[]

If you look at the Array functions, there are many other ways to do this, but the most recommended one could be changing the length.

As I said in the first place, you can also prototype remove() as it's the answer to your question. you can simply choose one of the methods above and prototype it to Array object in JavaScript, something like: