title: Optimize performances of text replacement with dictionary
body: <p>I'm developing an extension for Firefox. </p>
  
  <p>To summarize let's say it's a spell checker applied to a page with a lot of mistakes.</p>
  
  <p><strong>But it's very slow because there are too many steps.</strong></p>
  
  <p><strong>(1)</strong> I create a TreeWalker that filters only not-empty text nodes</p>
  
  <pre><code>var tree = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT, {
    acceptNode: function(node) {
      if (node.parentNode.nodeName !== "SCRIPT" &amp;&amp; node.nodeValue.trim().length &gt; 0) {
        return NodeFilter.FILTER_ACCEPT;
      }
    }
  }, false);
  </code></pre>
  
  <p><strong>(2)</strong> A while loop skims the TreeWalker</p>
  
  <p><strong>(3)</strong> In this while loop, a for loop skims the dictionary (which is quite big) with a complex <code>RegEx</code></p>
  
  <pre><code>const dico = [
    ['suffix1','suffix2','correction'],
    ['','',''],
    ['','',''],
    // a lot of lines
  ];
  </code></pre>
  
  <p><strong>(4)</strong> Text is replaced</p>
  
  <pre><code>dl = dico.length;
  
  function skim(){
    while (tree.nextNode()) {
      for (var i=0; i &lt; dl ; i++) {
        var r1 = new RegExp("([a-zÀ-ÖÙ-öù-üœŒ]+)?(" + dico[i][0] + ")[-/·∙.•](" + dico[i][1] + ")[-/·∙.•]?(s)?(?![a-z])", "gi");
        tree.currentNode.nodeValue = tree.currentNode.nodeValue.replace(r1, dico[i][3]);
      }
    }
  }
  </code></pre>
  
  <p>Plus, I'd like to use an other function that highlights the spelling mistakes, so that means adding a lot of Element Nodes between Text Nodes.</p>
  
  <p>How can I optimize that please ?  :)</p>
  
  <p>For the moment, the only solution I found is to simplify the dictionary but I I came to an end with this. And replace the <code>TreeWalker</code> by equivalents but it's faster.</p>
  
  <p>Thanks !</p>