indexOf with splice vs filter

Benchmark created on


Description

compare the speed of indexOf with splice vs filter

Setup

const keyToFind = "theMagic";
const startingArray = new Array(20000)
	.fill("something");
const randomIndex = Math.random() * 20000;
startingArray[randomIndex] = keyToFind;

Test runner

Ready to run.

Testing in
TestOps/sec
indexOf with splice
let myIterationArray = [...startingArray];
const index = startingArray.indexOf(keyToFind);
if (index !== -1) {
	startingArray.splice(index, 1);
}

ready
filter
myIterationArray = [...startingArray];
myIterationArray = myIterationArray.filter(i => i !== keyToFind);
ready

Revisions

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