From b3d0cce2c219553afdd79b7a3008b460078cfe1b Mon Sep 17 00:00:00 2001 From: camchenry <1514176+camchenry@users.noreply.github.com> Date: Wed, 16 Oct 2024 19:59:30 +0000 Subject: [PATCH] perf(linter/no-unescaped-entities): add fast path to check if char should be replaced (#6594) Flamegraph shows a lot of time spent hashing here, so I added a faster check to see if it is a char that we need to replace. --- crates/oxc_linter/src/rules/react/no_unescaped_entities.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/crates/oxc_linter/src/rules/react/no_unescaped_entities.rs b/crates/oxc_linter/src/rules/react/no_unescaped_entities.rs index 1fe0a52e15803..c45831c472f91 100644 --- a/crates/oxc_linter/src/rules/react/no_unescaped_entities.rs +++ b/crates/oxc_linter/src/rules/react/no_unescaped_entities.rs @@ -51,6 +51,9 @@ impl Rule for NoUnescapedEntities { if let AstKind::JSXText(jsx_text) = node.kind() { let source = jsx_text.span.source_text(ctx.source_text()); for (i, char) in source.char_indices() { + if !CHARS.contains(&char) { + continue; + } if let Some(escapes) = DEFAULTS.get(&char) { #[allow(clippy::cast_possible_truncation)] ctx.diagnostic(no_unescaped_entities_diagnostic( @@ -71,6 +74,9 @@ impl Rule for NoUnescapedEntities { } } +// NOTE: If we add substantially more characters, we should consider using a hash set instead. +pub const CHARS: [char; 4] = ['>', '"', '\'', '}']; + pub const DEFAULTS: Map = phf_map! { '>' => &[">"], '"' => &[""", "“", """, "”"],