Array of Objects Iteration (v2)

Revision 2 of this benchmark created on


Description

Turning an array of objects into a pointer driven list could -- and should -- make loops faster.

Setup

let itemArray = [];

for (let i = 0; i < 0x10000; i++) itemArray.push({ a : i });

// create Index
for (let i = 0; i < itemArray.length; i++) {
	itemArray[i].nextItem = itemArray[i + 1];
}

Teardown

document.body.textContent = '';

Test runner

Ready to run.

Testing in
TestOps/sec
Normal for loop
for (let i = 0, iLen = itemArray.length; i < iLen; i++) {
	document.body.append(
		itemArray[i].a,
		document.createElement("br")
	);
}
ready
Sibling walking
let item = itemArray[0];
if (item) do {
	document.body.append(
		item.a,
		document.createElement("br")
	);
} while (item = item.nextItem);
ready

Revisions

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