mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 12:19:15 +00:00
perf(ast): optimize JSXIdentifier::is_reference (#5344)
This commit is contained in:
parent
8a178075b7
commit
292f217da8
1 changed files with 13 additions and 1 deletions
|
|
@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue