AllMonths optimisation

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
var monthsObj = events.reduce(
			(list, { start, end }) => {
				var startDate = new Date(start);
				var endDate = new Date(end);

				var startName = startDate.toLocaleDateString('EN', {
					month: 'long',
				});
				var endName = endDate.toLocaleDateString('EN', {
					month: 'long',
				});
				const startNumber = `${startDate.getMonth() + 1}`.padStart(
					2,
					'0'
				); // january is 0 so we add 1
				const endNumber = `${endDate.getMonth() + 1}`.padStart(2, '0');
				const startId = startName.toLowerCase();
				const endId = endName.toLowerCase();

				if (!list[startNumber]) {
					list[startNumber] = { id: startId, title: startName };
				}

				if (!list[endNumber]) {
					list[endNumber] = { id: endId, 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.