Skip to content

Commit 346e143

Browse files
authored
perf: Replace if-else chain with switch-based token dispatch (#103)
closes #98
1 parent 6e95525 commit 346e143

File tree

1 file changed

+28
-35
lines changed

1 file changed

+28
-35
lines changed

src/tokenize.ts

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -116,41 +116,34 @@ export class Lexer {
116116
let start_column = this.column
117117

118118
// Fast path for single-character tokens
119-
if (ch === CHAR_LEFT_BRACE) {
120-
this.advance()
121-
return this.make_token(TOKEN_LEFT_BRACE, start, this.pos, start_line, start_column)
122-
}
123-
if (ch === CHAR_RIGHT_BRACE) {
124-
this.advance()
125-
return this.make_token(TOKEN_RIGHT_BRACE, start, this.pos, start_line, start_column)
126-
}
127-
if (ch === CHAR_COLON) {
128-
this.advance()
129-
return this.make_token(TOKEN_COLON, start, this.pos, start_line, start_column)
130-
}
131-
if (ch === CHAR_SEMICOLON) {
132-
this.advance()
133-
return this.make_token(TOKEN_SEMICOLON, start, this.pos, start_line, start_column)
134-
}
135-
if (ch === CHAR_COMMA) {
136-
this.advance()
137-
return this.make_token(TOKEN_COMMA, start, this.pos, start_line, start_column)
138-
}
139-
if (ch === CHAR_LEFT_BRACKET) {
140-
this.advance()
141-
return this.make_token(TOKEN_LEFT_BRACKET, start, this.pos, start_line, start_column)
142-
}
143-
if (ch === CHAR_RIGHT_BRACKET) {
144-
this.advance()
145-
return this.make_token(TOKEN_RIGHT_BRACKET, start, this.pos, start_line, start_column)
146-
}
147-
if (ch === CHAR_LEFT_PAREN) {
148-
this.advance()
149-
return this.make_token(TOKEN_LEFT_PAREN, start, this.pos, start_line, start_column)
150-
}
151-
if (ch === CHAR_RIGHT_PAREN) {
152-
this.advance()
153-
return this.make_token(TOKEN_RIGHT_PAREN, start, this.pos, start_line, start_column)
119+
switch (ch) {
120+
case CHAR_LEFT_BRACE:
121+
this.advance()
122+
return this.make_token(TOKEN_LEFT_BRACE, start, this.pos, start_line, start_column)
123+
case CHAR_RIGHT_BRACE:
124+
this.advance()
125+
return this.make_token(TOKEN_RIGHT_BRACE, start, this.pos, start_line, start_column)
126+
case CHAR_COLON:
127+
this.advance()
128+
return this.make_token(TOKEN_COLON, start, this.pos, start_line, start_column)
129+
case CHAR_SEMICOLON:
130+
this.advance()
131+
return this.make_token(TOKEN_SEMICOLON, start, this.pos, start_line, start_column)
132+
case CHAR_COMMA:
133+
this.advance()
134+
return this.make_token(TOKEN_COMMA, start, this.pos, start_line, start_column)
135+
case CHAR_LEFT_BRACKET:
136+
this.advance()
137+
return this.make_token(TOKEN_LEFT_BRACKET, start, this.pos, start_line, start_column)
138+
case CHAR_RIGHT_BRACKET:
139+
this.advance()
140+
return this.make_token(TOKEN_RIGHT_BRACKET, start, this.pos, start_line, start_column)
141+
case CHAR_LEFT_PAREN:
142+
this.advance()
143+
return this.make_token(TOKEN_LEFT_PAREN, start, this.pos, start_line, start_column)
144+
case CHAR_RIGHT_PAREN:
145+
this.advance()
146+
return this.make_token(TOKEN_RIGHT_PAREN, start, this.pos, start_line, start_column)
154147
}
155148

156149
// Whitespace

0 commit comments

Comments
 (0)