0

I have some code that does 4500 string replacement operations.

I measure how long this code takes to execute using performance.now() before and after.

Sometimes it takes 20-30 ms to execute, and sometimes it takes 1550-1600 ms.

I cannot figure out why the performance can be so random. Is it some sort of CPU branch prediction failures or Node.js optimizations that somehow cache the replacement results?

// example `row`:
// row = { pattern: '/some words/', replacement: 'some other words' }
for (const row of replacements) {
    // Avoid replacing with the word "undefined":
    if (!row.replacement) { row.replacement = ''; }

    const regex = new RegExp(row.pattern, 'gi');
    modifiedText = modifiedText.replace(regex, row.replacement);
}
  • Do your patterns contain special chars that you want to treat as literal symbols? E.g. `*`, `?`, `+` are special chars and they may match quite different texts than you expect and may malform the patterns, make them invalid, or very inefficient. – Wiktor Stribiżew Feb 20 '19 at 10:37
  • I am actually testing with exactly the same input string, so it is weird that the results would be different? And the patterns only contain a `$` sometimes, but very few of them. Most patterns are only like `/some word/` –  Feb 20 '19 at 10:38
  • Are the benchmarks done with the same input text **and** same `replacements` list ? – Gabriele Petrioli Feb 20 '19 at 10:44
  • @GabrielePetrioli yep, exactly same input (just a string `hi`) and the same replacements list. –  Feb 20 '19 at 10:45
  • can you please post the benchmarking code as well ? – Gabriele Petrioli Feb 20 '19 at 10:51
  • `const start = performance.now(); < the code above >; const end = performance.now(); console.log(Math.round(end - start));` –  Feb 20 '19 at 10:53
  • So, `$` may be inside the search terms, then you must [escape these strings](https://stackoverflow.com/a/3561711/3832970) to use them in `RegExp` constructor safely. – Wiktor Stribiżew Feb 20 '19 at 10:58

0 Answers0