loop vs foreach (v2)

Revision 2 of this benchmark created on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/chance/1.1.6/chance.min.js"></script>

Setup

const randomStrings = chance.unique(chance.string.bind(chance, {length: 10, casing: 'upper', alpha: true, numeric: true }), 250);
const sourceData = {};

randomStrings.forEach((str, index) => {
	sourceData[str] = {
		id: str,
		index,
	};
});

const arrOfNumbers = [1,2,3];


Test runner

Ready to run.

Testing in
TestOps/sec
.entries + foreach
const updatedData = {};
const sourceDataIds = Object.keys(sourceData);

if (sourceDataIds.length !== 0) {
	for (const dataId of sourceDataIds) {
		updatedData[dataId] = {
		...updatedData[dataId],
		isIndexEven: (sourceData[dataId].index % 2) === 0,
		xxx: false ? "false" : undefined,
		yyy: (sourceData[dataId].index % 2) ? [] : arrOfNumbers,
	};
	}
}
ready
.keys + loop
const updatedData = {};
const sourceDataIds = Object.keys(sourceData);

if (sourceDataIds.length !== 0) {
	for (let i = 0; i < sourceDataIds.length; i++) {
		const dataId = sourceDataIds[i];
		updatedData[dataId] = {
		...updatedData[dataId],
		isIndexEven: (sourceData[dataId].index % 2) === 0,
		xxx: false ? "false" : undefined,
		yyy: (sourceData[dataId].index % 2) ? [] : arrOfNumbers,
	};
	}
}
ready

Revisions

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