Length equal 0 or new array

JavaScript performance comparison

Revision 9 of this test case created


It should test what is more efficient way to clear an array.

Creating NEW with:

a = [];

or make the length equal 0 like this:

a.length = 0;

or pop all elements from the array like this:

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

NOTE : These methods are not equal in their behaviour! First one (a = []) will make all NEW array, and the others modify the current one! It is very important if you HAVE other references to that array!

Preparation code

Benchmark.prototype.setup = function() {
    a = [1,2,3,4,5,6,7,7];

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
New array method
a = [];
length equal to 0 method
a.length = 0;
Defining length isn't like usual "resetting" value
a.randomProperty = 0  // see the performance difference with setting length
Emptying array
while(a.length > 0) {

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:


Add a comment