findIndex (v5)

Revision 5 of this benchmark created on


Setup

const N = 10_000
const needle = 5_000

const A = []
for (let i = 0; i < N; i++) {
	A.push({ i })
}

const B = new Array(N)
for (let i = 0; i < N; i++) {
	B[i] = { i }
}

const C = []
for (let i = 0; i < N; i++) {
	C.push(i)
}

Array.prototype.manualFindIndex = function(pred) {
	for (let i = 0; i < this.length; i++) {
		if (pred(this[i])) return i
	}
}

Array.prototype.iterFindIndex = function(pred) {
	for (const [i, o] of this.entries()) {
		if (pred(o)) return i
	}
}

Test runner

Ready to run.

Testing in
TestOps/sec
findIndex
A.findIndex((o) => o.i === needle)
ready
manual
A.manualFindIndex((o) => o.i === needle)
ready
iter
A.iterFindIndex((o) => o.i === needle)
ready
findIndexSparse
B.findIndex((o) => o.i === needle)
ready
manualSparse
B.manualFindIndex((o) => o.i === needle)
ready
iterSparse
B.iterFindIndex((o) => o.i === needle)
ready
findIndexSmallInteger
C.findIndex((i) => i === needle)
ready
manualSmallInteger
C.manualFindIndex((i) => i === needle)
ready
iterSmallInteger
C.iterFindIndex((i) => i === needle)
ready

Revisions

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