I am trying to avoid a potential problem when I want to highlight all shortened links within a page. At the moment I have this code:
$('a').each(function() {
var urlHref = $(this).attr('href');
var shortenedLinks = ["bit.ly", "amzn.to","goo.gl","t.co","lnkd.in"];
for (var item in shortenedLinks) {
if (urlHref.includes(shortenedLinks[item])) {
console.log('yes');
} else {
console.log('no');
}
}
});
This works fine as the result in the console is:
yes
(4) no
The potential Problem
At the moment, for every link it runs through the array and compares part of the URL. This is fine for a small amount of URLS however I have about 80 URLshortener services, and what if the page contained 80 links that would be 6,400 checks.
I tried this with a few links on the page and all the shortened url providers and it took a while to go through them all.
Question
Is there a way to speed up this process so I can check the array and part of the url without looping through?