Object vs Check

Benchmark created on


Preparation HTML

<script>
var items = [1,2,3,4,5,6,7];
var allowedItems = [1,2,3];
var allowedItemsSet = new Set(allowedItems);
var allowedItemsObject = allowedItems.reduce((acc, curr) => {
    return {...acc, [curr]: true }
}, {});;


function getRandomItem() {
	return items[Math.floor(Math.random()*items.length)];	
}

function getItem() {
	return getRandomItem();
}

function ifCheck(item) {
	if (item === allowedItems[0] || item === allowedItems[1] || item === allowedItems[2] ) {
		return true;
	}
	return false;
}

function objCheck(item) {
	if (allowedItemsObject[item]) {
		return true;
	}
	return false;
}

function includesCheck(item) {
	if (allowedItems.includes(item)) {
		return true;
	}
	return false;
}

function setCheck(item) {
	if (allowedItemsSet.has(item)) {
		return true;
	}
	return false;
}

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
If statements
ifCheck(getItem());
ready
Hash
objCheck(getItem());
ready
Array.includes
includesCheck(getItem());
ready
Set
setCheck(getItem())
ready

Revisions

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