title: How can you filter out related words in large list quickly
body: <p>I am running this code on <a href="https://github.com/dwyl/english-words" rel="nofollow noreferrer">~479k words</a> and it is taking a very long time:</p>
  <pre><code>const fs = require('fs')
  
  const words = fs.readFileSync('dicts/eng.csv', 'utf-8')
    .split(/\n/)
    .filter(x =&gt; x &amp;&amp; !x.match('-') &amp;&amp; !x.match(/[A-Z]/))
    .reduce((m, x) =&gt; {
      m[x] = true
      return m
    }, {})
  
  for (let word in words) {
    for (let form in words) {
      if (form.indexOf(word) == 0) {
        if (form.length == word.length + 2 &amp;&amp; form.endsWith('ed')) {
          delete words[form]
        } else if (form.length == word.length + 4 &amp;&amp; form.endsWith('tion')) {
          delete words[form]
        } else if (form.length == word.length + 3 &amp;&amp; form.endsWith('ing')) {
          delete words[form]
        } else if (form.length == word.length + 2 &amp;&amp; form.endsWith('er')) {
          delete words[form]
        } else if (form.length == word.length + 2 &amp;&amp; form.endsWith('or')) {
          delete words[form]
        } else if (form.length == word.length + 3 &amp;&amp; form.endsWith('est')) {
          delete words[form]
        } else if (form.length == word.length + 1 &amp;&amp; form.endsWith('s')) {
          delete words[form]
        } else if (form.length == word.length + 3 &amp;&amp; form.endsWith('ers')) {
          delete words[form]
        } else if (form.length == word.length + 4 &amp;&amp; form.endsWith('ings')) {
          delete words[form]
        }
      }
    }
  }
  
  fs.writeFileSync('dicts/eng.out.csv', Object.keys(words).sort().join('\n'))
  </code></pre>
  <p>How can I speed this up to take only a fraction of the time, on the order of a second or two or whatever is more realistic?</p>
  <p>Is there some data structure I need to convert this list into that is better suited to a faster algorithm?</p>