From f82bc54be6ba0de6cae9b6112d1295bd08dcb52d Mon Sep 17 00:00:00 2001 From: rameel Date: Sun, 5 Apr 2026 01:49:31 +0500 Subject: [PATCH 1/3] Cache getters to avoid redundant recompilation --- src/plugins/format/index.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/plugins/format/index.js b/src/plugins/format/index.js index 8e9314e..40dd902 100644 --- a/src/plugins/format/index.js +++ b/src/plugins/format/index.js @@ -82,8 +82,17 @@ function plugin({ directive, evaluateLater, mutateDom }) { for (let attr of node.attributes) { const matches = [...attr.value.matchAll(placeholder_regex)]; if (matches.length) { + const getters = new Map( + matches.map(m => [ + m.groups.expr, + create_getter( + evaluateLater, + node, + m.groups.expr)])); + const template = attr.value; - update(() => attr.value = template.replace(placeholder_regex, (_, expr) => create_getter(evaluateLater, node, expr)())); + + update(() => attr.value = template.replace(placeholder_regex, (_, expr) => getters.get(expr)())); } } } From 0df7f23dabda415d23817e6b8eeaf060d82844eb Mon Sep 17 00:00:00 2001 From: rameel Date: Sun, 5 Apr 2026 02:58:00 +0500 Subject: [PATCH 2/3] Use fresh regex instance to avoid possible state issues --- src/plugins/format/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/plugins/format/index.js b/src/plugins/format/index.js index 40dd902..a5976c9 100644 --- a/src/plugins/format/index.js +++ b/src/plugins/format/index.js @@ -3,7 +3,7 @@ import { has_modifier } from "@/utilities/utils"; function plugin({ directive, evaluateLater, mutateDom }) { directive("format", (el, { modifiers }, { effect }) => { - const placeholder_regex = /{{(?.+?)}}/g; + const placeholder_regex = () => /{{(?.+?)}}/g; const is_once = has_modifier(modifiers, "once"); const has_format_attr = el => el.hasAttribute("x-format"); @@ -55,7 +55,7 @@ function plugin({ directive, evaluateLater, mutateDom }) { } function process_text_node(node) { - const tokens = node.textContent.split(placeholder_regex); + const tokens = node.textContent.split(placeholder_regex()); if (tokens.length > 1) { const fragment = new DocumentFragment(); @@ -80,7 +80,7 @@ function plugin({ directive, evaluateLater, mutateDom }) { function process_attributes(node) { for (let attr of node.attributes) { - const matches = [...attr.value.matchAll(placeholder_regex)]; + const matches = [...attr.value.matchAll(placeholder_regex())]; if (matches.length) { const getters = new Map( matches.map(m => [ @@ -92,7 +92,7 @@ function plugin({ directive, evaluateLater, mutateDom }) { const template = attr.value; - update(() => attr.value = template.replace(placeholder_regex, (_, expr) => getters.get(expr)())); + update(() => attr.value = template.replace(placeholder_regex(), (_, expr) => getters.get(expr)())); } } } From e4e1e07a328365a4dbb49e53be494b244f1322d8 Mon Sep 17 00:00:00 2001 From: rameel Date: Sun, 5 Apr 2026 03:05:34 +0500 Subject: [PATCH 3/3] Inline regex --- src/plugins/format/index.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/plugins/format/index.js b/src/plugins/format/index.js index a5976c9..058aa6e 100644 --- a/src/plugins/format/index.js +++ b/src/plugins/format/index.js @@ -3,7 +3,6 @@ import { has_modifier } from "@/utilities/utils"; function plugin({ directive, evaluateLater, mutateDom }) { directive("format", (el, { modifiers }, { effect }) => { - const placeholder_regex = () => /{{(?.+?)}}/g; const is_once = has_modifier(modifiers, "once"); const has_format_attr = el => el.hasAttribute("x-format"); @@ -55,7 +54,7 @@ function plugin({ directive, evaluateLater, mutateDom }) { } function process_text_node(node) { - const tokens = node.textContent.split(placeholder_regex()); + const tokens = node.textContent.split(/{{(?.+?)}}/g); if (tokens.length > 1) { const fragment = new DocumentFragment(); @@ -80,7 +79,7 @@ function plugin({ directive, evaluateLater, mutateDom }) { function process_attributes(node) { for (let attr of node.attributes) { - const matches = [...attr.value.matchAll(placeholder_regex())]; + const matches = [...attr.value.matchAll(/{{(?.+?)}}/g)]; if (matches.length) { const getters = new Map( matches.map(m => [ @@ -92,7 +91,7 @@ function plugin({ directive, evaluateLater, mutateDom }) { const template = attr.value; - update(() => attr.value = template.replace(placeholder_regex(), (_, expr) => getters.get(expr)())); + update(() => attr.value = template.replace(/{{(?.+?)}}/g, (_, expr) => getters.get(expr)())); } } }