Fastest way to delete an item from array

Benchmark created on


Setup

function *gen() {
	for (let i = 0; i < 1000; i++)
		yield `test${i}val`
}

let arr = new Array(...gen())
const lookup = 'test699val'

Test runner

Ready to run.

Testing in
TestOps/sec
Using delete-indexOf and flat
delete arr[arr.indexOf(lookup)]
arr = arr.flat()
ready
Using filter
arr = arr.filter((val) => val !== lookup)
ready
Using toSpliced
arr = arr.toSpliced(arr.indexOf(lookup), 1)
ready
Using separate slices
const ix = arr.indexOf(lookup)
arr = arr.slice(0, ix).concat(arr.slice(ix + 1))
ready
Using separate slices (oneline)
arr = arr.slice(0, arr.indexOf(lookup)).concat(arr.slice(arr.indexOf(lookup) + 1))
ready

Revisions

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