filter option lists before or after

Benchmark created on


Setup

const availableOption = {
	name: 'alpha',
	choices: [
		{
			name: 'one',
			id: 1,
			selected: false,
			quantity: 0,
			fractionalQuantity: 0,
		},
		{
			name: 'two',
			id: 2,
			selected: false,
			quantity: 0,
			fractionalQuantity: 0,
		},
		{
			name: 'three',
			id: 3,
			selected: false,
			quantity: 0,
			fractionalQuantity: 0,
		},
		{
			name: 'four',
			id: 4,
			selected: false,
			quantity: 0,
			fractionalQuantity: 0,
		},
		{
			name: 'five',
			id: 5,
			selected: false,
			quantity: 0,
			fractionalQuantity: 0,
		},
		{
			name: 'six',
			id: 6,
			selected: false,
			quantity: 0,
			fractionalQuantity: 0,
		},
	],
};

const givenOption = {
	name: 'beta',
	choices: [
		{
			name: 'three',
			id: 3,
			selected: true,
			quantity: 0,
			fractionalQuantity: 0,
		},
		{
			name: 'two',
			id: 2,
			selected: false,
			quantity: 0,
			fractionalQuantity: 0,
		},
		{
			name: 'six',
			id: 6,
			selected: false,
			quantity: 0,
			fractionalQuantity: 0,
		},
		{
			name: 'one',
			id: 1,
			selected: false,
			quantity: 0,
			fractionalQuantity: 0,
		},
		{
			name: 'five',
			id: 5,
			selected: false,
			quantity: 0,
			fractionalQuantity: 0,
		},
		{
			name: 'four',
			id: 4,
			selected: false,
			quantity: 0,
			fractionalQuantity: 0,
		},
	],
};

const availableOptions = [
	{...availableOption},
	{...availableOption},
	{...availableOption},
	{...availableOption},
	{...availableOption},
];

const givenOptions = [
	{...givenOption},
	{...givenOption},
	{...givenOption},
	{...givenOption},
	{...givenOption},
];

function choiceIsSelected (choice) {
	return choice.selected || choice.quantity > 0 || choice.fractionalQuantity > 0;
}

function choiceIsUserSet(choice) {
	const isIncluded = false;
	const isSelected = choiceIsSelected(choice);
	return (isSelected && !isIncluded) || (!isSelected && isIncluded);
}

Test runner

Ready to run.

Testing in
TestOps/sec
filter after
availableOptions.forEach(availOpt => {
	const selectedOption = JSON.parse(JSON.stringify(givenOptions.find(
		selOpt => selOpt.optionId === availOpt.optionId
	)));

	if(selectedOption) {
		availOpt.choices = availOpt.choices.map(availableChoice => {
			const selectedChoiceIndex = selectedOption.choices.findIndex(chc => chc.choiceId === availableChoice.choiceId);

			if(selectedChoiceIndex !== -1) {
				const selectedChoice = selectedOption.choices[selectedChoiceIndex];

				const newChoice = {
					...availableChoice,
					fractionalQuantity: selectedChoice.fractionalQuantity,
					isUserSet: selectedChoice.isUserSet,
					quantity: selectedChoice.quantity,
					selected: selectedChoice.selected
				};

				selectedOption.choices.splice(selectedChoiceIndex, 1);

				return newChoice;
			} else {
				return availableChoice;
			}
		});

		if(selectedOption.choices.length > 0) {
			const remainingUserChoices = selectedOption.choices.filter(chc => choiceIsUserSet(chc));
			if(remainingUserChoices.length > 0) {
				console.error(`${selectedOption.choices.length} user option choice(s) remain unmatched.`);
			}
		}
	}
});
ready
filter before
availableOptions.forEach(availOpt => {
	const selectedOption = JSON.parse(JSON.stringify(givenOptions.find(
		selOpt => selOpt.optionId === availOpt.optionId
	)));

	if(selectedOption) {
		// creates a copy
		const selectedUserChoices = selectedOption.choices.filter(chc => choiceIsUserSet(chc));

		availOpt.choices = availOpt.choices.map(availableChoice => {
			const selectedChoiceIndex = selectedUserChoices.findIndex(chc => chc.choiceId === availableChoice.choiceId);

			if(selectedChoiceIndex !== -1) {
				const selectedChoice = selectedUserChoices[selectedChoiceIndex];

				const newChoice = {
					...availableChoice,
					fractionalQuantity: selectedChoice.fractionalQuantity,
					isUserSet: selectedChoice.isUserSet,
					quantity: selectedChoice.quantity,
					selected: selectedChoice.selected
				};

				selectedUserChoices.splice(selectedChoiceIndex, 1);

				return newChoice;
			} else {
				return availableChoice;
			}
		});

		if(selectedUserChoices.length > 0) {
			console.error(`${selectedOption.choices.length} user option choice(s) remain unmatched.`);
		}
	}
});
ready

Revisions

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