Clear Array Methods

JavaScript performance comparison

Test case created by John Pittman

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var arr1 = [];
  var arr2 = [];
  var arr3 = [];
  
  for (var i = 0; i < 1000; ++i) {
    arr1.push(i);
  }
  for (var i = 0; i < 10000; ++i) {
    arr2.push(i);
  }
  for (var i = 0; i < 20000; ++i) {
    arr3.push(i);
  }

};
</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
1000 - Setting the length to 0
arr1.length = 0;
pending…
1000 - Creating fresh array
arr1 = [];
pending…
1000 - Clear by looping the array and shifting
for (var i = 0, n = arr1.length; i < n; ++i) {
  arr1.shift();
}
pending…
10000 - Setting the length to 0
arr2.length = 0;
pending…
10000 - Creating fresh array
arr2 = [];
pending…
10000 - Clear by looping the array and shifting
for (var i = 0, n = arr2.length; i < n; ++i) {
  arr2.shift();
}
pending…
20000 - Setting the length to 0
arr3.length = 0;
pending…
20000 - Creating fresh array
arr3 = [];
pending…
20000 - Clear by looping the array and shifting
for (var i = 0, n = arr3.length; i < n; ++i) {
  arr3.shift();
}
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