Array reordering

Benchmark created on


Description

Comparison of splice- and slice-based array reordering routines

Setup

const length = 10000
const list = new Array(length)
for (let i = 0; i < length; i++) {
	list[i] = i + 1
}

const spliceBasedArrayReordering = (list, startIndex, endIndex) => {
  const result = Array.from(list)
  const [removed] = result.splice(startIndex, 1)
  result.splice(endIndex, 0, removed)

  return result
}

const sliceBasedArrayReordering = (list, startIndex, endIndex) => {
    const intermediate = [...list.slice(0, startIndex), ...list.slice(startIndex + 1)]
    const result = [...intermediate.slice(0, endIndex), list[startIndex], ...intermediate.slice(endIndex + 1)]
    return result
}

Test runner

Ready to run.

Testing in
TestOps/sec
Splice-based array reordering
spliceBasedArrayReordering(list, 5, 10)
ready
Slice-based array reordering
sliceBasedArrayReordering(list, 5, 10)
ready

Revisions

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