Empty JavaScript array redux

JavaScript performance comparison

Test case created by diadistis

Info

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

This version of the test is much slower because it needs to copy the pre-populated array in each iteration. I have included a control snippet to calculate the time needed for the copy.

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
control (small)
small.slice();
pending…
control (medium)
medium.slice();
pending…
control (big)
big.slice();
pending…
new array (small)
var arr = small.slice();
arr = [];
pending…
new array (medium)
var arr = medium.slice();
arr = [];
pending…
new array (big)
var arr = big.slice();
arr = [];
pending…
length = 0 (small)
var arr = small.slice();
arr.length = 0;
pending…
length = 0 (medium)
var arr = medium.slice();
arr.length = 0;
pending…
length = 0 (big)
var arr = big.slice();
arr.length = 0;
pending…
splice (small)
var arr = small.slice();
arr.splice(0, arr.length);
pending…
splice (medium)
var arr = medium.slice();
arr.splice(0, arr.length);
pending…
splice (big)
var arr = big.slice();
arr.splice(0, arr.length);
pending…
while;pop (small)
var arr = small.slice();
while(arr.length > 0) {
    arr.pop();
}
pending…
while;pop (medium)
var arr = medium.slice();
while(arr.length > 0) {
    arr.pop();
}
pending…
while;pop (big)
var arr = big.slice();
while(arr.length > 0) {
    arr.pop();
}
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Comment form temporarily disabled.

Add a comment