isHex (v6)

Revision 6 of this benchmark created on


Setup

const hexString = "deadd0d0";
const hexChars = "0123456789abcdef";

Test runner

Ready to run.

Testing in
TestOps/sec
regex
return hexString.match(/^[0-9a-f]$/i);
ready
negated regex
return hexString.match(/[^0-9a-f]/i);
ready
array includes
for(let c of hexString.toLowerCase()) {
	if( !hexChars.includes(c))
	return false;
}


return true;
ready
case sensitive regex
return hexString.toLowerCase().match(/^[0-9a-f]$/);
ready
individual char codes
for(let c in hexString.toLowerCase()) {
	if(!(hexString.charCodeAt(c) >= 48 && hexString.charCodeAt(c) <= 57) && !(hexString.charCodeAt(c) >= 97 && hexString.charCodeAt(c) <= 102)) {
		return false;
	}
}

return true;
ready
char codes in array
const codes = [];

for(let c in hexString) codes.push(hexString.charCodeAt(c));

for(let c of codes) {
	if(!(c >= 48 && c <= 57) && !(c >= 97 && c <= 102)) {
		return false;
	}
}

return true;
ready
to number and back
return Number(parseInt(hexString,16)).toString(16) === hexString;
ready

Revisions

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