compare array elements and remove the match

Benchmark created on


Description

In this test, both arrays are in the same order.

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: '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,
		},
	],
};

Test runner

Ready to run.

Testing in
TestOps/sec
splice
availableOption.choices = availableOption.choices.map(availableChoice => {
	const selectedChoiceIndex = givenOption.choices.findIndex(chc => chc.choiceId === availableChoice.choiceId);

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

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

		givenOption.choices.splice(selectedChoiceIndex, 1);

		return newChoice;
	} else {
		return availableChoice;
	}
});
ready
conditional unshift
availableOption.choices = availableOption.choices.map(availableChoice => {
	const selectedChoiceIndex = givenOption.choices.findIndex(chc => chc.choiceId === availableChoice.choiceId);

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

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

		if(selectedChoiceIndex === 0) {
			givenOption.choices.unshift();
		} else {
			givenOption.choices.splice(selectedChoiceIndex, 1);
		}

		return newChoice;
	} else {
		return availableChoice;
	}
});
ready
filter
availableOption.choices = availableOption.choices.map(availableChoice => {
	const selectedChoiceIndex = givenOption.choices.findIndex(chc => chc.choiceId === availableChoice.choiceId);

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

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

		givenOption.choices = givenOption.choices.filter(chc => chc.choiceId !== availableChoice.choiceId);
		
		return newChoice;
	} else {
		return availableChoice;
	}
});
ready

Revisions

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