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

JavaScript performance comparison

Revision 147 of this test case created by David Scott and last updated

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  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();
  }

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/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;
pending…
splice (5000 elements)
var arr = large_array();

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

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

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

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

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

Compare results of other browsers

Revisions

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

0 Comments