Test find vs for loop for multiples

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
find
const devices = [
	{
		position: 'front',
	},
	{
		position: 'back',
	},
	{
		position: 'external',
	},
	{
		position: 'unspecified',
	}
];

const front = devices.find(device => device.position === 'front');
const external = devices.find(device => device.position === 'external');
const back = devices.find(device => device.position === 'back');
const unspecified = devices.find(device => device.position === 'unspecified');
ready
for loop
const devices = [
	{
		position: 'front',
	},
	{
		position: 'back',
	},
	{
		position: 'external',
	},
	{
		position: 'unspecified',
	}
];

let front;
let back;
let unspecified;
let external;

for (const device of devices) {
	switch(device.position) {
		case 'front':
			front = device;
			break;
		case 'back':
			back = device;
			break;
		case 'unspecified':
			unspecified = device;
			break;
		case 'external':
			external = device;
			break;
		default:
			throw new Error('Unspecified device position');
	}
}
ready

Revisions

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