Loop summing if array contains odd or even

Benchmark created on


Description

see speed of different ways to loop

Setup

const array = []
for (let i = 0; i <= 100; i++) {
	array[i] = i
}

let odd = 0
let even = 0

Test runner

Ready to run.

Testing in
TestOps/sec
1
array.forEach((element) => {
	if (element % 2 == 0) {
		even++
	} else {
		odd++
	}
})
ready
2
for (let element of array) {
	if (element % 2 == 0) {
		even++
	} else {
		odd++
	}
}
ready
3
for (let i = 0; i < array.length; i++) {
	if (array[i] % 2 == 0) {
		even++
	} else {
		odd++
	}
}
ready
4
for (const i in array) {
	if (array[i] % 2 == 0) {
		even++
	} else {
		odd++
	}
}
ready

Revisions

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