Empty JavaScript array (v9)

Revision 9 of this benchmark created by serjout on


Description

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

Setup

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());
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Overwriting with a new array (small)
small = [];
ready
Overwriting with a new array (medium)
medium = [];
ready
Overwriting with a new array (big)
big = [];
ready
Setting length to 0 (small)
small.length = 0;
ready
Setting length to 0 (medium)
medium.length = 0;
ready
Setting length to 0 (big)
big.length = 0;
ready
Using splice (small)
small.splice(0, small.length);
ready
Using splice (medium)
medium.splice(0, medium.length);
ready
Using splice (big)
big.splice(0, big.length);
ready
Using while, pop (small)
while (small.length > 0) {
    small.pop();
}
ready
Using while, pop (medium)
while (medium.length > 0) {
    medium.pop();
}
ready
Using while, pop (big)
while (big.length > 0) {
    big.pop();
}
ready
Using while, unshift (small)
while (small.length > 0) {
    small.unshift();
}
ready

Revisions

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