Run details:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36
Chrome 95
Linux
Desktop
3 years ago
Test name Executions per second
Delete by Splice 201777.6 Ops/sec
Delete by copyWithin 204637.6 Ops/sec
Delete by Filter 41877.0 Ops/sec
HTML Preparation code:
AخA
 
1
<script>
2
  var array = [];
3
  for(let i = 0; i < 1000; i++) {array[i] = i;}
4
</script>
Script Preparation code:
x
 
/* these functions assume that only one element matches, so they do not loop! */
function deleteBySplice (array, element) {
  var index = array.indexOf( element );
  if (index !== -1) {
    array.splice( index, 1 );
  }
}
function deleteByCopyWithin (array, element) {
  var index = array.indexOf( element );
  if (index !== -1) {
    array.copyWithin( index, index + 1 );
    --array.length;
  }
}
function deleteByFilter (array, element) {
  array = array.filter( el => el !== element );
}
Tests:
  • Delete by Splice

     
    deleteBySplice( array, 21 );
    deleteBySplice( array, 304 );
    deleteBySplice( array, 777 );
  • Delete by copyWithin

     
    deleteByCopyWithin( array, 21 );
    deleteByCopyWithin( array, 304 );
    deleteByCopyWithin( array, 777 );
  • Delete by Filter

     
    deleteByFilter( array, 21 );
    deleteByFilter( array, 304 );
    deleteByFilter( array, 777 );