splice vs slice when removing element (v9)

Revision 9 of this benchmark created on


Description

Check what's better to remove an element from an array

Preparation HTML

<script>
 
  var a = [], i = 1000;
  while (i > 0) {
    a.push({v:Math.random(), v1:i})
    i--;
  }

  var fn = function(arr, index){

    var len = arr.length;
    var new_arr = new Array(len-1);
    var i = 0, count = 0;
    while(i<len) {
     if(i!==index){
      new_arr[count] = arr[i];
      count++;
     }
     i++;
    }
    return new_arr;
  };

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
slice
var b = Object.create(a);
var sliced = b.slice(0, 500).concat(b.slice(501))
ready
splice
var b = Object.create(a);
var spliced = b.splice(500, 1); // <--- cause change on the original array!!!
ready
copy function
var b = Object.create(a);
var copied = fn(b, 500);
ready

Revisions

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