arr modify on copy

Benchmark created on


Setup

const arr = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", ]

Test runner

Ready to run.

Testing in
TestOps/sec
spread
const newArr = [
...arr.slice(0, 5), 
"X", 
...arr.slice(6)
]
ready
for push
const newArr = []

for(let i = 0; i < arr.length; i++) {
	if (i === 5) {
		newArr.push("X")		
	} else {
		newArr.push(arr[i])		
	}

}
ready
fixed
const newArr = new Array(arr.length)

for(let i = 0; i < newArr.length; i++) {
	if (i === 5) {
		newArr[i] = "X"
	} else {
		newArr[i] = arr[i]
	}
}
ready
map
const newArr = arr.map((o, i) => i === 5 ? "X" : o)
ready

Revisions

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