Generator vs arrays

Benchmark created on


Setup

const pointsCount = 1000

Test runner

Ready to run.

Testing in
TestOps/sec
simple lists (naive repeated push)
function generatePoints() {
	const points = []
	for (let i = 0; i < pointsCount; i++) {
		points.push(i)
	}
	return points
}

function generateCompound() {
	const firstSet = generatePoints()
	const [, ...secondSet] = generatePoints()
	const result = [...firstSet, ...secondSet]
	
	return result
}


function iterateResult() {
	let sum = 0
	for (const point of generateCompound()) {
		sum += point
	}
	
	return sum
}

console.log(iterateResult())
ready
generators
function* generatePoints() {
	for (let i = 0; i < pointsCount; i++) {
		yield i
	}
}

function* generateCompound() {
	yield* generatePoints()
	
	const secondGenerator = generatePoints()
	// skip first entry
	secondGenerator.next()
	
	yield* secondGenerator	
}

function iterateResult() {
	let sum = 0
	for (const point of generateCompound()) {
		sum += point
	}
	
	return sum
}

console.log(iterateResult())
ready

Revisions

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