Test if filter is better than splice

Benchmark created on


Setup

let array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];

Test runner

Ready to run.

Testing in
TestOps/sec
Filter
array = array.filter(a => a % 2 != 0);
ready
Splice
for(let i = 0; i < array.length; i++) {
	if(array[i] % 2 === 0) {
		array.splice(i, 1);
	}
}
ready
Splice with cache
const l = array.length;
for(let i = 0; i < l; i++) {
	if(array[i] % 2 === 0) {
		array.splice(i, 1);
	}
}
ready

Revisions

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