Skip to content

Commit 9228801

Browse files
authored
perf: shave off some bytes by using peek() more often (#105)
flowing out of testing #99
1 parent 346e143 commit 9228801

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/tokenize.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export class Lexer {
158158
this.advance(2) // Skip /*
159159
while (this.pos < this.source.length - 1) {
160160
let ch = this.source.charCodeAt(this.pos)
161-
if (ch === CHAR_ASTERISK && this.source.charCodeAt(this.pos + 1) === CHAR_FORWARD_SLASH) {
161+
if (ch === CHAR_ASTERISK && this.peek() === CHAR_FORWARD_SLASH) {
162162
this.advance(2)
163163
break
164164
}
@@ -179,25 +179,22 @@ export class Lexer {
179179
if (is_digit(ch)) {
180180
return this.consume_number(start_line, start_column)
181181
}
182+
182183
if (ch === CHAR_DOT && is_digit(this.peek())) {
183184
return this.consume_number(start_line, start_column)
184185
}
185186

186187
// CDO: <!--
187188
if (ch === CHAR_LESS_THAN && this.pos + 3 < this.source.length) {
188-
if (
189-
this.source.charCodeAt(this.pos + 1) === CHAR_EXCLAMATION &&
190-
this.source.charCodeAt(this.pos + 2) === CHAR_HYPHEN &&
191-
this.source.charCodeAt(this.pos + 3) === CHAR_HYPHEN
192-
) {
189+
if (this.peek() === CHAR_EXCLAMATION && this.peek(2) === CHAR_HYPHEN && this.peek(3) === CHAR_HYPHEN) {
193190
this.advance(4)
194191
return this.make_token(TOKEN_CDO, start, this.pos, start_line, start_column)
195192
}
196193
}
197194

198195
// CDC: -->
199196
if (ch === CHAR_HYPHEN && this.pos + 2 < this.source.length) {
200-
if (this.source.charCodeAt(this.pos + 1) === CHAR_HYPHEN && this.source.charCodeAt(this.pos + 2) === CHAR_GREATER_THAN) {
197+
if (this.peek() === CHAR_HYPHEN && this.peek(2) === CHAR_GREATER_THAN) {
201198
this.advance(3)
202199
return this.make_token(TOKEN_CDC, start, this.pos, start_line, start_column)
203200
}
@@ -263,7 +260,7 @@ export class Lexer {
263260

264261
while (this.pos < this.source.length - 1) {
265262
let ch = this.source.charCodeAt(this.pos)
266-
if (ch === CHAR_ASTERISK && this.source.charCodeAt(this.pos + 1) === CHAR_FORWARD_SLASH) {
263+
if (ch === CHAR_ASTERISK && this.peek() === CHAR_FORWARD_SLASH) {
267264
this.advance(2)
268265
break
269266
}
@@ -353,7 +350,7 @@ export class Lexer {
353350
this.pos < this.source.length &&
354351
this.source.charCodeAt(this.pos) === CHAR_DOT &&
355352
this.pos + 1 < this.source.length &&
356-
is_digit(this.source.charCodeAt(this.pos + 1))
353+
is_digit(this.peek())
357354
) {
358355
this.advance() // .
359356
while (this.pos < this.source.length && is_digit(this.source.charCodeAt(this.pos))) {
@@ -412,7 +409,7 @@ export class Lexer {
412409
// Check what follows the backslash before consuming it
413410
if (this.pos + 1 >= this.source.length) break
414411

415-
let next = this.source.charCodeAt(this.pos + 1)
412+
let next = this.peek()
416413

417414
// If followed by newline, it's invalid, stop without consuming backslash
418415
if (is_newline(next)) break

0 commit comments

Comments
 (0)