Empty JavaScript array (v13)

Revision 13 of this benchmark created 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
123
var l = big.length;
while (l) {
  big.pop();
}
ready

Revisions

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