Length equal 0 or new array

JavaScript performance comparison

Test case created by Paweł Brewczyński


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;

NOTE : Those two methods are not equal in their behaviour! First one (a = []) will make all NEW array, and second one (a.length = 0) modify current one! I 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

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