Length equal 0 or new array

JavaScript performance comparison

Revision 11 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;

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

 
<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…
Popping off items from array
while (a.length > 0) {
  a.pop();
}
pending…
Random
a.randomProperty = 0;
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