title: Most efficient way to check what keywords (10k words) appear in an array of strings?
body: <p>I have several arrays of 100 strings which contain comments that I have retrieved using fetch. (Called <code>comments</code>) and another array containing 10,000 keywords. (Called <code>keywords</code>)</p>
  <p>For each comment in the array I want to check which of the keywords it contains. (I need to know all of the keywords it contains but do not need to know how many times it occurs)</p>
  <p>What is the fastest way to do this?</p>
  <p>I have tried <code>forEach</code> loops within each other:</p>
  <pre><code>keywords.forEach(word =&gt; {
      comments.forEach(comment =&gt; {
         if(comment.includes(word)) //call a function
      }
  })
  </code></pre>
  <p>as well as <code>for</code> loops within each other:</p>
  <pre><code>for(i = 0; i &lt; keywords.length; i++) {
      for(j = 0; j &lt; comments.length; j++){
           if(comments[j].includes(keywords[i])) //call a function
      }
  }
  </code></pre>
  <p>For both of these I have tried switching the inner and outer loops.</p>
  <p>I have also tried building a regular expression with my keywords and matching against that using <code>matchAll</code> and a <code>for..of</code> loop.</p>
  <pre><code>keywords.forEach(word =&gt; regex = regex + `(^|\\b)${word}(\\b|$)|`)
  comments.forEach(comment =&gt; {
      const matches = comment.matchAll(regex)
      for (const match of matches){
         //call a function
      }
  })
  </code></pre>
  <p>These all worked on my initial test of ~10 keywords but obviously it is going to take a lot longer with 10,000 keywords.  What is the most efficient way of doing this?</p>
  <p>This is all quite new to me so there is a chance I am missing something obvious!</p>
  <p>Thank you</p>