DEV Community

Discussion on: Clean up your code with these tips!

vojtechp profile image

Regex is not much readable when there are more strings.

const isLocal = domain && /^(localhost|127\.0\.0\.1)$/.test(domain);

This is for me better solution:

const localDomains = [ 'localhost', '' ];
const isLocal = domain && localDomains.includes(domain);
dechamp profile image
DeChamp Author

This is also a very nice solution. I’ll add it to the tips! Thank you. I know that for some people, regex can be a eyesore. But there is beauty in its power.

miniscruff profile image

Not much difference but I like to use sets for these comparisons for faster lookups.

const localDomains = new Set([...])
Thread Thread
dechamp profile image
DeChamp Author

I actually also like this as a solution. Thank you!