From eefc08f1330e1ba473893b4092bea80412c3a1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 3 Sep 2019 10:41:43 -0400 Subject: [PATCH 1/3] refactor: early return on hot path --- packages/babel-parser/src/parser/comments.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/babel-parser/src/parser/comments.js b/packages/babel-parser/src/parser/comments.js index bdeac9c140cb..2f512ed904e9 100644 --- a/packages/babel-parser/src/parser/comments.js +++ b/packages/babel-parser/src/parser/comments.js @@ -39,10 +39,11 @@ export default class CommentsParser extends BaseParser { } adjustCommentsAfterTrailingComma(node: Node, elements: Node[]) { - if (elements.length === 0) { + if (this.state.leadingComments.length === 0) { return; } - if (this.state.leadingComments.length === 0) { + + if (elements.length === 0) { return; } From 973aefe99b514e4fc3b48e233a6652250f980cad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 3 Sep 2019 21:17:50 -0400 Subject: [PATCH 2/3] refactor: read next2 only when next is dot --- packages/babel-parser/src/tokenizer/index.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index ffb8cba8ca6f..0faa56043f34 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -404,8 +404,10 @@ export default class Tokenizer extends LocationParser { return; } - const next2 = this.input.charCodeAt(this.state.pos + 2); - if (next === charCodes.dot && next2 === charCodes.dot) { + if ( + next === charCodes.dot && + this.input.charCodeAt(this.state.pos + 2) === charCodes.dot + ) { this.state.pos += 3; this.finishToken(tt.ellipsis); } else { From 90966405e076b62bd204eca60db470d968add6a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Wed, 4 Sep 2019 22:35:04 -0400 Subject: [PATCH 3/3] perf: mini refactor --- packages/babel-parser/src/tokenizer/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/babel-parser/src/tokenizer/index.js b/packages/babel-parser/src/tokenizer/index.js index 0faa56043f34..f3e9a0438023 100644 --- a/packages/babel-parser/src/tokenizer/index.js +++ b/packages/babel-parser/src/tokenizer/index.js @@ -234,8 +234,8 @@ export default class Tokenizer extends LocationParser { skipBlockComment(): void { const startLoc = this.state.curPosition(); const start = this.state.pos; - const end = this.input.indexOf("*/", (this.state.pos += 2)); - if (end === -1) this.raise(this.state.pos - 2, "Unterminated comment"); + const end = this.input.indexOf("*/", this.state.pos + 2); + if (end === -1) this.raise(start, "Unterminated comment"); this.state.pos = end + 2; lineBreakG.lastIndex = start; @@ -902,9 +902,9 @@ export default class Tokenizer extends LocationParser { let val; if (this.hasPlugin("numericSeparator")) { - const prev = this.input.charCodeAt(this.state.pos - 1); - const next = this.input.charCodeAt(this.state.pos + 1); if (code === charCodes.underscore) { + const prev = this.input.charCodeAt(this.state.pos - 1); + const next = this.input.charCodeAt(this.state.pos + 1); if (allowedSiblings.indexOf(next) === -1) { this.raise(this.state.pos, "Invalid or unexpected token"); }