html-minifier-next 4.8.0 → 4.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/htmlparser.js CHANGED
@@ -76,6 +76,14 @@ const nonPhrasing = new CaseInsensitiveSet(['address', 'article', 'aside', 'base
76
76
 
77
77
  const reCache = {};
78
78
 
79
+ // Pre-compiled regexes for common special elements (`script`, `style`, `noscript`)
80
+ // These are used frequently and pre-compiling them avoids regex creation overhead
81
+ const preCompiledStackedTags = {
82
+ 'script': /([\s\S]*?)<\/script[^>]*>/i,
83
+ 'style': /([\s\S]*?)<\/style[^>]*>/i,
84
+ 'noscript': /([\s\S]*?)<\/noscript[^>]*>/i
85
+ };
86
+
79
87
  function attrForHandler(handler) {
80
88
  let pattern = singleAttrIdentifier.source +
81
89
  '(?:\\s*(' + joinSingleAttrAssigns(handler) + ')' +
@@ -224,7 +232,8 @@ export class HTMLParser {
224
232
  prevTag = '';
225
233
  } else {
226
234
  const stackedTag = lastTag.toLowerCase();
227
- const reStackedTag = reCache[stackedTag] || (reCache[stackedTag] = new RegExp('([\\s\\S]*?)</' + stackedTag + '[^>]*>', 'i'));
235
+ // Use pre-compiled regex for common tags (`script`, `style`, `noscript`) to avoid regex creation overhead
236
+ const reStackedTag = preCompiledStackedTags[stackedTag] || reCache[stackedTag] || (reCache[stackedTag] = new RegExp('([\\s\\S]*?)</' + stackedTag + '[^>]*>', 'i'));
228
237
 
229
238
  html = await replaceAsync(html, reStackedTag, async (_, text) => {
230
239
  if (stackedTag !== 'script' && stackedTag !== 'style' && stackedTag !== 'noscript') {