spread vs splice

Benchmark created on


Setup

const pointCount = 1000

const getFirstArray = () => Array.from({length: pointCount}, (_, idx) => idx)
const getSecondArray = () => Array.from({length: pointCount}, (_, idx) => idx)

function sumArray(array) {
	let sum = 0
	for (const value of array) {
		sum += value
	}
	
	return sum
}

Test runner

Ready to run.

Testing in
TestOps/sec
destructoring
const [, ...slicedArray] = getSecondArray()
const newArray = [...getFirstArray(), ...slicedArray]

const result = sumArray(newArray)
console.log('destructoring', result)
ready
splice second array
const splicedArray = getFirstArray()
splicedArray.splice(-1, 1)

const newArray = [...splicedArray, ...getSecondArray()]

const result = sumArray(newArray)
console.log('splice second array', result)
ready
splice everything
const newArray = getFirstArray()

newArray.splice(-1, 1, ...getSecondArray())

const result = sumArray(newArray)
console.log('everything', result)
ready
concat vs destructoring
const firstArray = getFirstArray()
firstArray.splice(-1, 1)

const newArray = firstArray.concat(...getSecondArray())

const result = sumArray(newArray)
console.log('concat', result)
ready

Revisions

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