Compare regex vs direct access

Benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Regex
const NIE_REGEX = /^[XYZ]/i;

const dni = '05642355P',
	nie =  'X0134567A';
	

if (NIE_REGEX.test(dni)) {
	return 'NIE';
}


if (NIE_REGEX.test(nie)) {
	return 'NIE';
}
ready
Bracket access
const dni = '05642355P',
	nie =  'X0134567A';
	
const dniFirstLetter = dni[0];
if (dniFirstLetter === 'X' || dniFirstLetter === 'Y' || dniFirstLetter === 'Z') {
	return 'NIE';
}

const nieFirstLetter = nie[0];
if (nieFirstLetter === 'X' || nieFirstLetter === 'Y' || nieFirstLetter === 'Z') {
	return 'NIE';
}
ready
With At
const dni = '05642355P',
	nie =  'X0134567A';
	
const dniFirstLetter = dni.at(0);
if (dniFirstLetter === 'X' || dniFirstLetter === 'Y' || dniFirstLetter === 'Z') {
	return 'NIE';
}

const nieFirstLetter = nie.at(0);
if (nieFirstLetter === 'X' || nieFirstLetter === 'Y' || nieFirstLetter === 'Z') {
	return 'NIE';
}
ready

Revisions

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