Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move the regex declaration outside the function #15

Closed
morellodev opened this issue Aug 11, 2021 · 2 comments · Fixed by #16
Closed

Move the regex declaration outside the function #15

morellodev opened this issue Aug 11, 2021 · 2 comments · Fixed by #16

Comments

@morellodev
Copy link
Contributor

Hi @sindresorhus, this might be a very little performance improvement, but it makes the code even clearer. Instead of inlining the regex for testing absolute urls inside the body of the function, it would be better to extract it outside and then referencing it:

const ABSOLUTE_URL_REGEX = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/;

export default function isAbsoluteUrl(url) {
  ...
  return ABSOLUTE_URL_REGEX.test(url);
}
@sindresorhus
Copy link
Owner

I doubt it makes a difference performance-wise, but I agree it's cleaner this way.

@morellodev
Copy link
Contributor Author

Yes this does not make great difference in terms of performance, but now those regexes are created once and reused every time the function is called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants