Check if an array value exists as part of the url I have the following bit of code in a service worker that checks to see it the url contains any of the values in an array and if so it breaks out of the function.  As it is run for every asset on a webpage, it can be run a lot of times so I was wondering if it could be optimised in any way:


const urlsToIgnore = [
  'boldchat.com',
  'pinimg.com',
  'responsetap.com',
  'facebook.net',
  'dwin1.com',
  'bing.com',
  'google-analytics.com',
  'googletagmanager.com',
  'hotjar.com',
  'pinterest.com',
];

self.addEventListener('fetch', event =&gt; {
  // don't load assets from these urls
  for (let i = 0; i &lt; urlsToIgnore.length; i++) {
    if (event.request.url.indexOf(urlsToIgnore[i]) &gt; -1) {
      return;
    }
  }
  
  // .... rest of code to cache the assets and page
});
If you can use ES6 Features in your code, use a Set for constant lookup time:
const urlsToIgnore = new Set([
  'boldchat.com',
  'pinimg.com',
  'responsetap.com',
  'facebook.net',
  'dwin1.com',
  'bing.com',
  'google-analytics.com',
  'googletagmanager.com',
  'hotjar.com',
  'pinterest.com',
]);

// later
if (uslstoIgnore.has(event.request.url) { ... }

Otherwise, just use a simple object as a map:
const urlsToIgnore = {
  'boldchat.com': 1,
  'pinimg.com': 1,
  'responsetap.com': 1,
  'facebook.net': 1,
  'dwin1.com': 1,
  'bing.com': 1,
  'google-analytics.com': 1,
  'googletagmanager.com': 1,
  'hotjar.com': 1,
  'pinterest.com': 1,
};

// later
if (uslstoIgnore[event.request.url]) { ... }

Both these options offer constant time lookup, as opposed to the linear time of your current solution.
The actual performance gain depends on the size of the list with ignored items, but since this list can be added to, I'd opt for an O(1) lookup.