Skip to content

Commit

Permalink
perf(ast): faster Comment::is_jsdoc (#7905)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
overlookmotel committed Dec 15, 2024
1 parent b99ee37 commit a5f04a7
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions crates/oxc_ast/src/ast/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a5f04a7

Please sign in to comment.