Empty an array (corrected, plus large and small arrays) (v147)

Revision 147 of this benchmark created by David Scott on


Setup

var _small_array = [];
  for(var i=0; i < 50; ++i) _small_array.push({a: 1, b: 2});
  
  var _large_array = [];
  for(var i=0; i < 5000; ++i) _large_array.push({a: 1, b: 2});
  
  function small_array() {
    return _small_array.slice();
  }
  
  function large_array() {
    return _large_array.slice();
  }

Test runner

Ready to run.

Testing in
TestOps/sec
length (5000 elements)
var arr = large_array();

/* set length to 0 deletes each index >= 0 (ECMA-262 15.4.5.1) */
arr.length = 0;
ready
splice (5000 elements)
var arr = large_array();

/* splice(0, length) removes everything and returns a copy */
arr.splice(0, arr.length);
ready
pop (5000 elements)
var arr = large_array();

/* pop the final element of the array until empty */
while (arr.length > 0) {
  arr.pop();
}
ready
length (50 elements)
var arr = small_array();

/* set length to 0 deletes each index >= 0 (ECMA-262 15.4.5.1) */
arr.length = 0;
ready
splice (50 elements)
var arr = small_array();

/* splice(0, length) removes everything and returns a copy */
arr.splice(0, arr.length);
ready
pop (50 elements)
var arr = small_array();

/* pop the final element of the array until empty */
while (arr.length > 0) {
  arr.pop();
}
ready

Revisions

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