Skip to content

Commit 3fa87ff

Browse files
authored
perf(lexer): peak 2 bytes after ! (#8662)
`!==` and `!=` is very frequent. <img width="845" alt="image" src="https://github.com/user-attachments/assets/91ff20fc-ae1c-4fb9-9444-4eb90d8e95f3" /> Picked this up while profiling.
1 parent 878ce10 commit 3fa87ff

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

crates/oxc_parser/src/lexer/byte_handlers.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,28 @@ ascii_byte_handler!(LIN(lexer) {
209209
// !
210210
ascii_byte_handler!(EXL(lexer) {
211211
lexer.consume_char();
212-
if lexer.next_ascii_byte_eq(b'=') {
213-
if lexer.next_ascii_byte_eq(b'=') {
214-
Kind::Neq2
215-
} else {
216-
Kind::Neq
212+
if let Some(next_2_bytes) = lexer.peek_2_bytes() {
213+
match next_2_bytes[0] {
214+
b'=' => {
215+
if next_2_bytes[1] == b'=' {
216+
lexer.consume_2_chars();
217+
Kind::Neq2
218+
} else {
219+
lexer.consume_char();
220+
Kind::Neq
221+
}
222+
}
223+
_ => Kind::Bang
217224
}
218225
} else {
219-
Kind::Bang
226+
// At EOF, or only 1 byte left
227+
match lexer.peek_byte() {
228+
Some(b'=') => {
229+
lexer.consume_char();
230+
Kind::Neq
231+
}
232+
_ => Kind::Bang
233+
}
220234
}
221235
});
222236

0 commit comments

Comments
 (0)