From 3c150e6dd73d5d8011cba1a1b6a899fe0a21caa5 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Sun, 22 Sep 2024 07:08:00 +0800 Subject: [PATCH] perf: reduce string clone in HastVisitor --- crates/core/src/hast_to_swc_ast/mod.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/crates/core/src/hast_to_swc_ast/mod.rs b/crates/core/src/hast_to_swc_ast/mod.rs index fa848fd..0b27afd 100644 --- a/crates/core/src/hast_to_swc_ast/mod.rs +++ b/crates/core/src/hast_to_swc_ast/mod.rs @@ -75,8 +75,8 @@ fn text(n: &swc_xml::ast::Text) -> Option { static ref SPACE_REGEX: Regex = Regex::new(r"^\s+$").unwrap(); } - let value = n.data.to_string(); - if SPACE_REGEX.is_match(&value) { + let value = n.data.as_str(); + if SPACE_REGEX.is_match(value) { return None; } @@ -84,7 +84,7 @@ fn text(n: &swc_xml::ast::Text) -> Option { span: DUMMY_SP, expr: JSXExpr::Expr(Box::new(Expr::Lit(Lit::Str(Str { span: DUMMY_SP, - value: decode_xml(&value).into(), + value: decode_xml(value).into(), raw: None, })))), })) @@ -112,7 +112,7 @@ impl HastVisitor { .attributes .iter() .map(|attr| { - let value = attr.value.clone().map(|v| get_value(&attr.name, &v)); + let value = attr.value.as_ref().map(|v| get_value(&attr.name, v)); JSXAttrOrSpread::JSXAttr(JSXAttr { span: DUMMY_SP, name: JSXAttrName::Ident(self.get_key(&attr.name, &n.tag_name).into()), @@ -128,23 +128,23 @@ impl HastVisitor { )); let children = self.all(&n.children); - let opening = JSXOpeningElement { - span: DUMMY_SP, - name: name.clone(), - attrs, - self_closing: children.is_empty(), - type_args: None, - }; - let closing = if !children.is_empty() { Some(JSXClosingElement { span: DUMMY_SP, - name, + name: name.clone(), }) } else { None }; + let opening = JSXOpeningElement { + span: DUMMY_SP, + name: name, + attrs, + self_closing: children.is_empty(), + type_args: None, + }; + JSXElement { span: DUMMY_SP, opening,