Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(ast): optimize JSXIdentifier::is_reference #5344

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion crates/oxc_ast/src/ast_impl/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,20 @@ impl<'a> JSXIdentifier<'a> {
///
/// References begin with a capital letter, `_` or `$`.
/// <https://babeljs.io/repl#?code_lz=DwMQ9mAED0B8DcAoYAzCMHIPpqnJwAJLhkkA&presets=react>
// `name.chars().next().unwrap()` cannot panic because name is never an empty string.
#[allow(clippy::missing_panics_doc)]
pub fn is_reference(&self) -> bool {
self.name.chars().next().map_or(false, |c| c.is_uppercase() || c == '_' || c == '$')
// The identifier has already been checked to be valid, so when first char is ASCII, it can only
// be `a-z`, `A-Z`, `_` or `$`. But compiler doesn't know that, so we can help it create faster
// code by taking that invariant into account.
// `b < b'a'` matches `A-Z`, `_` and `$`.
// Use a fast path for common case of ASCII characters, to avoid the more expensive
// `char::is_uppercase` in most cases.
let name = self.name.as_str();
match name.as_bytes()[0] {
b if b.is_ascii() => b < b'a',
_ => name.chars().next().unwrap().is_uppercase(),
}
}
}

Expand Down