Loop through HTMLCollection (v2)

Revision 2 of this benchmark created on


Preparation HTML

<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>
<div class="text">Test test test test</div>

Setup

const collection = document.getElementsByClassName("text");
let x;

const loopSize = 1000;

Test runner

Ready to run.

Testing in
TestOps/sec
Spread operator
for (let j = 0; j < loopSize; j++)
{
	const list = [...collection];
	for (let i = 0; i < list.length; i++)
		x = list[i];
}
ready
Array from
for (let j = 0; j < loopSize; j++)
{
	const list = Array.from(collection);
	for (let i = 0; i < list.length; i++)
		x = list[i];
}
ready
Random access
for (let j = 0; j < loopSize; j++)
{
	for (let i = 0; i < collection.length; i++)
		x = collection[i];
}
ready
For...of
for (let j = 0; j < loopSize; j++)
{
	for (const element of collection)
		x = element;
}
ready

Revisions

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