Empty JavaScript array

JavaScript performance comparison

Test case created by Gajus

Info

Original discussion at http://stackoverflow.com/questions/1232040/how-to-empty-an-array-in-javascript.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var small = [],
        medium = [],
        big = [],
        smallSize = 100,
        mediumSize = 10000,
        bigSize = 100000;
   
    while (smallSize--) {
        small.push(Math.random());
    }
   
    while (mediumSize--) {
        medium.push(Math.random());
    }
   
    while (bigSize--) {
        big.push(Math.random());
    }
};
</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
Overwriting with a new array (small)
small = [];
pending…
Overwriting with a new array (medium)
medium = [];
pending…
Overwriting with a new array (big)
big = [];
pending…
Setting length to 0 (small)
small.length = 0;
pending…
Setting length to 0 (medium)
medium.length = 0;
pending…
Setting length to 0 (big)
big.length = 0;
pending…
Using splice (small)
small.splice(0, small.length);
pending…
Using splice (medium)
medium.splice(0, medium.length);
pending…
Using splice (big)
big.splice(0, big.length);
pending…
Using while, pop (small)
while (small.length > 0) {
    small.pop();
}
pending…
Using while, pop (medium)
while (medium.length > 0) {
    medium.pop();
}
pending…
Using while, pop (big)
while (big.length > 0) {
    big.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