From 511d5e58ff9aa4c0a3bd17f07b42e830709a599c Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Thu, 26 Mar 2026 13:42:31 +0000 Subject: [PATCH] perf(parser): add `Modifiers::get` method (#20741) Add a `get` method to `Modifiers` that does a fast bitflags check whether the modifier is present before doing slower iteration through the `Vec`. The fast path is always taken except when there's a syntax error (rare). --- crates/oxc_parser/src/js/class.rs | 2 +- crates/oxc_parser/src/modifiers.rs | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/oxc_parser/src/js/class.rs b/crates/oxc_parser/src/js/class.rs index da6407bc7528a..e2ac7498e934b 100644 --- a/crates/oxc_parser/src/js/class.rs +++ b/crates/oxc_parser/src/js/class.rs @@ -462,7 +462,7 @@ impl<'a, C: Config> ParserImpl<'a, C> { modifiers: &Modifiers<'a>, decorators: Vec<'a, Decorator<'a>>, ) -> ClassElement<'a> { - if let Some(modifier) = modifiers.iter().find(|m| m.kind == ModifierKind::Declare) { + if let Some(modifier) = modifiers.get(ModifierKind::Declare) { self.error(diagnostics::declare_constructor(modifier.span)); } diff --git a/crates/oxc_parser/src/modifiers.rs b/crates/oxc_parser/src/modifiers.rs index 368c84c64b1ed..72293e7b85ce4 100644 --- a/crates/oxc_parser/src/modifiers.rs +++ b/crates/oxc_parser/src/modifiers.rs @@ -207,6 +207,17 @@ impl<'a> Modifiers<'a> { self.modifiers.as_ref().into_iter().flat_map(|modifiers| modifiers.iter()) } + /// Look up a specific modifier by [`ModifierKind`]. + pub fn get(&self, kind: ModifierKind) -> Option<&Modifier> { + if self.kinds.contains(kind) { + let modifier = self.iter().find(|m| m.kind == kind); + debug_assert!(modifier.is_some()); + modifier + } else { + None + } + } + pub fn accessibility(&self) -> Option { self.kinds.accessibility() }