Files
webawesome/docs/_utilities/replacer.cjs
Konnor Rogers 5219188690 Reduce time it takes to replace strings (#6)
* reduce replacer time from 9 seconds to 3 seconds

* prettier

* prettier
2023-09-12 14:46:39 -04:00

25 lines
746 B
JavaScript

/**
* @typedef {object} Replacement
* @property {string | RegExp} pattern
* @property {string} replacement
*/
/**
* @typedef {Array<Replacement>} Replacements
*/
/**
* @param {Document} content
* @param {Replacements} replacements
*/
module.exports = function (content, replacements) {
/** This seems trivial, but by assigning to a string first, THEN using innerHTML after iterating over every replacement, we reduce the calculations of JSDOM. At the time of writing benchmarks show a reduction from 9seconds to 3 seconds by doing so. */
let html = content.body.innerHTML;
replacements.forEach(replacement => {
html = html.replaceAll(replacement.pattern, replacement.replacement);
});
content.body.innerHTML = html;
};