filter vs in-place

Benchmark created on


Setup

const arr_l = 1000;
const rand_idx = Math.floor(Math.random()*1000);
let registries = new Array(arr_l);

for (let i=0; i<1000; i++) {
	if (i===rand_idx) {
		registries[i] = "needle";
	} else {
		registries[i] = "miss";
	}
}

Test runner

Ready to run.

Testing in
TestOps/sec
filter
registries = registries.filter((registry) => {
	if (registry === "needle") {
		return false;
	}
	return true;
})
ready
in-place
for (let j=0;j<arr_l; j++) {
	const curr = registries[j];
	if (curr === "needle") {
		registries.splice(j, 1)
	}
}
ready

Revisions

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