Length equal 0 or new array

JavaScript performance comparison

Revision 9 of this test case created

Info

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

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

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 = [];
pending…
length equal to 0 method
a.length = 0;
pending…
Defining length isn't like usual "resetting" value
a.randomProperty = 0  // see the performance difference with setting length
pending…
Emptying array
while(a.length > 0) {
    a.pop();
}
pending…

Compare results of other browsers

Revisions

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:

0 comments

Add a comment