You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[utils] RegEx is slow (can we make faster hex functions?) #10208
function isHexAddressCaseInsensitiveFast(str) {
if (typeof str !== "string" || str.length !== 42) return false;
if (str[0] !== '0' || str[1] !== 'x') return false;
for (let i = 2; i < 42; i++) {
// transform the character code for case-insensitivity
const charCode = str.charCodeAt(i) & 0xDF;
// after transformation, '0'-'9' are in the range 16-25
const numberRange = (charCode - 16) | (25 - charCode);
// after transformation, 'a'-'f' are in the range 65-70 ('A'-'F'), `A`-`F` remains unchanged
const letterRange = (charCode - 65) | (70 - charCode);
// identifies if the charCode is OUTSIDE of both valid ranges.
if (((numberRange & letterRange) & 0x80000000) !== 0) {
return false;
}
}
return true;
}
for a 50% spread of 1000000 valid/invalid mix-cased strings the runtimes are:
@davidmurdoch I thought your view was that these changes to the hex functions were in the end micro-optimizations? Do you think it's still worth it to optimize them further?
changed the title [-]RegEx is slow (can we make faster hex functions?)[/-][+][utils] RegEx is slow (can we make faster hex functions?)[/+]on Sep 11, 2026
Activity
[-]RegEx is slow[/-][+]RegEx is slow (can we make faster hex functions?)[/+]davidmurdoch commentedon Jul 8, 2025
I got into some further optimizations:
for a 50% spread of 1000000 valid/invalid mix-cased strings the runtimes are:
isHexAddressCaseInsensitive: 238.701ms (uses `HEX_CHECKSUM_ADDRESS_REGEX`) isHexAddressCaseInsensitiveFast: 50.609msOk, I'm going to stop now. Use bitwise responsibly. :-)
mcmire commentedon Jul 9, 2025
@davidmurdoch I thought your view was that these changes to the hex functions were in the end micro-optimizations? Do you think it's still worth it to optimize them further?
[-]RegEx is slow (can we make faster hex functions?)[/-][+][utils] RegEx is slow (can we make faster hex functions?)[/+]