Array performance (v2)

Revision 2 of this benchmark created on


Setup

const list = Array.from({ length: 10000 }, (_, val) => val + 1)
const lowTarget = 1127
const highTarget = 9116

function forIn(list, target) {
	for (const val in list) {
		if (val === target) {
			return val
		}
	}
}

function forOf(list, target) {
	for (const val of list) {
		if (val === target) {
			return val
		}
	}
}

function findReversed(list, target) {
	for (let i = list.length - 1; i >= 0; --i) {
		if (list[i] === target) {
			return list[i]
		}
	}
}

function find(list, target) {
	for (let i = 0; i < list.length; i++) {
		if (list[i] === target) {
			return list[i]
		}
	}
	
}

Test runner

Ready to run.

Testing in
TestOps/sec
forIn (low)
forIn(list, lowTarget )
ready
forIn (high)
forIn(list, highTarget)
ready
forOf (low)
forOf(list, lowTarget)
ready
forOf (high)
forOf(list, highTarget)
ready
for-loop (low)
find(list, lowTarget)
ready
for-loop (high)
find(list, highTarget)
ready
for-loop reversed (low)
findReversed(list, lowTarget)
ready
for-loop reversed (high)
findReversed(list, highTarget)
ready

Revisions

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