Splice is the fastest method
const array = [0, 1, 2, 3, 4, 5];
const indicesToRemove = [0, 2, 5];
for (let index = indicesToRemove.length; index >= 0 ; index--) {
array.splice(index, 1);
}
and if you work with a lot of data and make new arrays, such as filter or push, you will have memory problems.
Benchmarking:
// setup
const array1 = [...new Array(5000).keys()];
const indicesToRemove1 = [...new Array(5000).keys()];
const array2 = [...new Array(5000).keys()];
const indicesToRemove2 = [...new Array(5000).keys()];
const array3 = [...new Array(5000).keys()];
const indicesToRemove3 = [...new Array(5000).keys()];
const array4 = [...new Array(5000).keys()];
const indicesToRemove4 = new Set([...new Array(5000).keys()]);
test 1
for (let index = indicesToRemove1.length; index >= 0 ; index--) {
array1.splice(index, 1);
}
// 3,398 ops/s ±0.71%
// fastest
test 2
for (let index = indicesToRemove2.length; index--;) {
array2.splice(index, 1);
}
// 3,377 ops/s ±0.53%
// 0.62% slower
test 3
const newArray = array4.filter((_,i) => !indicesToRemove4.has(i))
// 3,322 ops/s ±0.5%
// 2.25% slower
test 4
const newArray = array3.filter((_,i) => !indicesToRemove3.includes(i))
// 22 ops/s ±5.6%
// 99.35% slower