What’s the fastest method to detect if foo='http://john.doe' is an external url (in comparsion to window.location.href)?
What’s the fastest method to detect if foo=’http://john.doe’ is an external url (in comparsion
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Update: I did some more research and found that using
new URLis easily fast enough, and IMO the most straight-forward way of doing this.It is important to note that every method I’ve tried takes less than 1ms to run even on an old phone. So performance shouldn’t be your primary consideration unless you are doing some large batch processing. Use the regex version if performance is your top priority.
These are the three methods I tried:
new URL:String.replace:
Regex:
Here are some basic tests I used to test performance: https://is-external-url-test.glitch.me/