Empty JavaScript array (v10)

Revision 10 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 = [];
small.push(777);
ready
Overwriting with a new array (medium)
medium = [];
medium.push(777);
ready
Overwriting with a new array (big)
big = [];
big.push(777);
ready
Setting length to 0 (small)
small.length = 0;
small.push(777);
ready
Setting length to 0 (medium)
medium.length = 0;
medium.push(777);
ready
Setting length to 0 (big)
big.length = 0;
big.push(777);
ready
Using splice (small)
small.splice(0, small.length);
small.push(777);
ready
Using splice (medium)
medium.splice(0, medium.length);
medium.push(777);
ready
Using splice (big)
big.splice(0, big.length);
big.push(777);
ready
Using while, pop (small)
while (small.length > 0) {
    small.pop();
}
small.push(777);
ready
Using while, pop (medium)
while (medium.length > 0) {
    medium.pop();
}
medium.push(777);
ready
Using while, pop (big)
while (big.length > 0) {
    big.pop();
}
big.push(777);
ready
Using while, shift (small)
while (small.length > 0) {
    small.shift();
}
small.push(777);
ready
Using while, shift (medium)
while (medium.length > 0) {
    medium.shift();
}
medium.push(777);
ready
Using while, shift (big)
while (big.length > 0) {
    big.shift();
}
big.push(777);
ready
Using slice (small)
small = small.slice(0,0)
small.push(777);
ready
Using slice (medium)
medium = medium.slice(0,0)
medium.push(777);
ready
Using slice (big)
big = big.slice(0,0)
big.push(777);
ready

Revisions

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