AllMonths optimisation (v2)

Revision 2 of this benchmark created on


Setup

const events = Array.from({ length: 1000 }, () => {
  const start = new Date(2023, Math.floor(Math.random() * 12), Math.floor(Math.random() * 28) + 1).toISOString();
  const end = new Date(2023, Math.floor(Math.random() * 12), Math.floor(Math.random() * 28) + 1).toISOString();
  return { start, end };
});

const orderArrayByKey = (array, key) => {
		array.sort(function (a, b) {
			if (a[key] < b[key]) {
				return -1;
			}
			if (a[key] > b[key]) {
				return 1;
			}
			return 0;
		});
	};
 

Test runner

Ready to run.

Testing in
TestOps/sec
Initial
let allMonths = (() => {
	let monthsList = [];
	events.forEach((event) => {
		let { start, end } = event,
			startDate = new Date(start),
			endDate = new Date(end),
			startMonth = startDate.toLocaleDateString('EN', {month: 'numeric'}),
			endMonth = endDate.toLocaleDateString('EN', {month: 'numeric'});

			if ( startDate && startMonth && monthsList.filter((item) => String(item.id).toLowerCase().padStart(2, '0') ===	String(startMonth).toLowerCase().padStart(2, '0')).length === 0) {
				monthsList.push({
						id: String(startMonth).toLowerCase().padStart(2, '0'),
						title: startDate.toLocaleDateString('EN', {
							month: 'long',
						}),
						href: '',
						isActive: false,
					});
				}

				if (
					endDate &&
					endMonth &&
					monthsList.filter(
						(item) =>
							String(item.id).toLowerCase().padStart(2, '0') ===
							String(endMonth).toLowerCase().padStart(2, '0')
					).length === 0
				) {
					monthsList.push({
						id: String(endMonth).toLowerCase().padStart(2, '0'),
						title: endDate.toLocaleDateString('EN', {
							month: 'long',
						}),
						href: '',
						isActive: false,
					});
				}
			});

	return monthsList;
})();

orderArrayByKey(allMonths, 'id');

allMonths.unshift({
	id: '',
	title: 'Month (All)',
	isActive: false,
});
ready
Refactored
const monthsObj = events.reduce(
			(list, { start, end }) => {
				const startDate = new Date(start);
				const endDate = new Date(end);

				const startNumber = `${startDate.getMonth() + 1}`.padStart(
					2,
					'0'
				); // january is 0 so we add 1
				const endNumber = `${endDate.getMonth() + 1}`.padStart(2, '0');

				if (!list[startNumber]) {
					const startName = startDate.toLocaleDateString('EN', {
						month: 'long',
					});
					list[startNumber] = {
						id: startName.toLowerCase(),
						title: startName,
					};
				}

				if (!list[endNumber]) {
					const endName = endDate.toLocaleDateString('EN', {
						month: 'long',
					});
					list[endNumber] = {
						id: endName.toLowerCase(),
						title: endName,
					};
				}

				return list;
			},
			// Initial list with the placeholder. It's an object to easily check if a month is already listed
			{
				all: { id: '', title: 'Month (All)' },
			}
		);

		var allMonths = Object.keys(monthsObj)
			.sort()
			.reduce((acc, key) => [...acc, monthsObj[key]], []);
ready

Revisions

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