From a5f04a72b5db5fda743d758c0964f03009661a70 Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Sun, 15 Dec 2024 04:28:27 +0000 Subject: [PATCH] perf(ast): faster `Comment::is_jsdoc` (#7905) Small optimization. Replace string slice + `starts_with` (at least 3 x bounds checks + 2 x UTF8 character boundary checks) with a single byte read (1 x bounds check). --- crates/oxc_ast/src/ast/comment.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/oxc_ast/src/ast/comment.rs b/crates/oxc_ast/src/ast/comment.rs index fa2f6b4fbda59..8e1a33cf101a7 100644 --- a/crates/oxc_ast/src/ast/comment.rs +++ b/crates/oxc_ast/src/ast/comment.rs @@ -104,9 +104,10 @@ impl Comment { /// Returns `true` if this comment is a JSDoc comment. Implies `is_leading` /// and `is_block`. pub fn is_jsdoc(&self, source_text: &str) -> bool { - self.is_leading() - && self.is_block() - && self.content_span().source_text(source_text).starts_with('*') + self.is_leading() && self.is_block() && { + let span = self.content_span(); + !span.is_empty() && source_text.as_bytes()[span.start as usize] == b'*' + } } /// Legal comments