Range

Benchmark created on


Setup

const range1 = (from, to) => {
	return new Array(to - from + 1)
		.fill()
		.map((_, i) => i + from)
}

const range2 = (from, to) => {
	return new Array(to - from + 1)
		.fill(0)
		.map((_, i) => i + from)
}

const range3 = (from, to) => {
	return Array.from(
		{length: to - from + 1},
		(_, i) => i + from
	)
}

const range4 = (from, to) => {
	const arr = []
	for (let i = from; i <= to; ++i) {
		arr.push(i)
	}
	return arr
}

const range5 = (from, to) => {
	const len = to - from + 1
	const arr = new Array(len)
	for (let i = 0; i < len; ++i) {
		arr[i] = i + from
	}
	return arr
}

const range6 = (from, to) => {
	const len = to - from + 1
	const arr = []
	for (let i = 0; i < len; ++i) {
		arr[i] = i + from
	}
	return arr
}

Test runner

Ready to run.

Testing in
TestOps/sec
new array fill
range1(3, 10000)
ready
new array fill 0
range2(3, 10000)
ready
array from
range3(3, 10000)
ready
for loop push
range4(3, 10000)
ready
for loop prealloc
range5(3, 10000)
ready
for loop rely on auto-resize
range6(3, 10000)
ready

Revisions

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