Array push vs Array map vs For (v2)

Revision 2 of this benchmark created on


Setup

const array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Test runner

Ready to run.

Testing in
TestOps/sec
Array push
const temp = []
array.forEach(i => {
	const newI = i ** 2
	temp.push(newI)
})
ready
Array map
const temp = array.map(i => {
	const newI = i ** 2
	return newI
})
ready
For
const temp = []
const arrayLength = array.length
for (let i = 0; i < arrayLength; ++i) {
	const newI = array[i] ** 2
	temp[i] = newI
}
ready
For optimize 1
const temp = []
for (let i = array.length - 1; i >= 0; --i) {
	const newI = array[i] ** 2
	temp[i] = newI
}
ready
For optimize 2
const temp = []
for (let i = array.length - 1; i + 1; --i) {
	const newI = array[i] ** 2
	temp[i] = newI
}
ready
For optimize 3
const temp = []
for (let i = array.length; i--;) {
  const newI = array[i] ** 2
  temp[i] = newI
}
ready
For optimize 4
const temp = []
for (let i = array.length; --i + 1;) {
  const newI = array[i] ** 2
  temp[i] = newI
}
ready

Revisions

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