Fastest way to detect external URLs

Update: I did some more research and found that using new URL is 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:

const isExternalURL = (url) => new URL(url).origin !== location.origin;


function isExternalReplace(url) {
  const domain = (url) => url.replace('http://','').replace('https://','').split("")[0];       
  return domain(location.href) !== domain(url);


const isExternalRegex = (function(){
  const domainRe = /https?:\/\/((?:[\w\d-]+\.)+[\w\d]{2,})/i;

  return (url) => {
    const domain = (url) => domainRe.exec(url)[1];
    return domain(location.href) !== domain(url);

Here are some basic tests I used to test performance:

Leave a Comment