Array vs itterator

Benchmark created on


Setup

const levels = 8;
let markup = `<div></div>`.repeat(3);

for (let i = 0; i < levels; i++) {
	markup = `<div>${markup}</div>`.repeat(3);
}

const root = document.createElement('div');
root.innerHTML = markup;

Test runner

Ready to run.

Testing in
TestOps/sec
Array
function decendants(root) {
	const nodes = [];
	
	function walk(node) {
		for (const child of node.childNodes) {
			nodes.push(child);
			if (child.nodeType === 1) walk(child);
		}
	}
	
	walk(root);
	
	return nodes;
}

let i = 0;
for (const node of decendants(root)) {
	i++;
}
ready
Ittr
function* decendants(root) {
	for (const child of root.childNodes) {
		yield child;
		if (child.nodeType === 1) {
			yield* decendants(child);	
		}
	}
}

let i = 0;
for (const node of decendants(root)) {
	i++;
}
ready

Revisions

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