Array of Objects Iteration

Benchmark created on


Description

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

Setup

let itemArray = [
	{ a : 1 },
	{ a : 2 },
	{ a : 4 },
	{ a:  8 },
	{ a : 16 },
	{ a : 32 },
	{ a : 64 },
	{ a : 128 }
];

// 0x08 shl 0x0D == 0x00010000 entries
for (let i = 0; i < 0x0D; i++) itemArray = itemArray.concat(itemArray);

// 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.