Test name | Executions per second |
---|---|
Delete by Splice | 283760.3 Ops/sec |
Delete by copyWithin | 284425.8 Ops/sec |
Delete by Filter | 41340.5 Ops/sec |
<script>
var array = [];
for(let i = 0; i < 1000; i++) {array[i] = i;}
</script>
/* 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 );
}
deleteBySplice( array, 21 );
deleteBySplice( array, 304 );
deleteBySplice( array, 777 );
deleteByCopyWithin( array, 21 );
deleteByCopyWithin( array, 304 );
deleteByCopyWithin( array, 777 );
deleteByFilter( array, 21 );
deleteByFilter( array, 304 );
deleteByFilter( array, 777 );