for vs map

Benchmark created on


Setup

let a = new Array(100)
for (let i = 0; i < a.length; i++) {
	a[i] = (i + 10) * (i + 2)
}

function mappedFn(n) {
	return { 
		type: 'div', 
		children: ['item' + n]
	}
}

Test runner

Ready to run.

Testing in
TestOps/sec
map
let x = a.map(mappedFn)
ready
for
let x = []
for (let i = 0; i < a.length; i++) {
	x.push(mappedFn(a[i]))
}
ready
for with capacity
let x = new Array(a.length)
for (let i = 0; i < a.length; i++) {
	x[i] = mappedFn(a[i])
}
ready

Revisions

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