Based on your comments, it seems you have already been told that the most expensive is to restore the DOM, which occurs when you completely replace the entire contents of the page (i.e. when you assign document.body.innerHTML ). You do this for every lookup. This causes Firefox to redraw the entire page for each replacement you make. You only need to assign document.body.innerHTML once, after you have completed all the replacements.
The following should provide a first pass when accelerating:
function escapeRegExp(str) { return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"); } function convert2latin() { newInnerHTML = document.body.innerHTML for (let i = 0; i < Table.length; i++) { newInnerHTML = newInnerHTML.replace(new RegExp(escapeRegExp(Table[i][1]), 'g'), Table[i][0]); } document.body.innerHTML = newInnerHTML }
You note in the comments that there is no real need to use RegExp to match, so the following will be even faster:
function convert2latin() { newInnerHTML = document.body.innerHTML for (let i = 0; i < Table.length; i++) { newInnerHTML = newInnerHTML.replace(Table[i][1], Table[i][0]); } document.body.innerHTML = newInnerHTML }
If you really need to use RegExp to match, and you will perform these exact substitutions several times, you are better off creating all RegExp before first use (for example, when a Table is created / modified) and saving them (for example, in Table[i][2] )
However, assigning document.body.innerHTML is a bad way to do this:
As mentioned in 8472, replacing the entire contents of document.body.innerHTML is a very difficult task to accomplish this task, which has some significant drawbacks, including probably a violation of the functionality of other JavaScript on the page and possible security problems. A better solution would be to change only the textContent text nodes.
One way to do this is to use TreeWalker . The code for this might be something like this:
function convert2latin(text) { for (let i = 0; i < Table.length; i++) { text = text.replace(Table[i][1], Table[i][0]); } return text }