Array unshift vs other alternatives

Benchmark created on


Setup

const arr = []

const genItem = () => ({ id: Math.random().toString(), title: Math.random().toString(), cp: Math.random().toString() })

for (let i = 0; i < 100_000; i++) {
	arr.push(genItem())
}

Test runner

Ready to run.

Testing in
TestOps/sec
Adds element to the beginning with spread …
const arr2 = [{ id: '6f4dfs', title: 'My item', cp: '' }, ...arr];
ready
Adds element to the beginning with unshift()
arr.unshift({ id: '6f4dfs', title: 'My item', cp: '' })
ready
Adds element to the beginning with concat()
const arr2 = [{ id: '6f4dfs', title: 'My item', cp: '' }].concat(arr)
ready

Revisions

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