Run details:
Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.9 Safari/537.36
Chrome 62
Windows
Desktop
3 years ago
Test name Executions per second
Delete by Splice 151076.2 Ops/sec
Delete by copyWithin 151605.3 Ops/sec
Delete by Filter 85243.5 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 );