Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Miscellaneous perf tweak #10421

Merged
merged 3 commits into from Sep 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/babel-parser/src/parser/comments.js
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised by this change. Checking an array's length should be much faster than accessing 3 props.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking an array's length should be much faster than accessing 3 props.

True. This change is implemented from the code coverage result via running babel-parser on material-ui. There are 4000 exits from this.state.leadingComments.length === 0 but 400 exits on elements.length === 0, which means in real world it is more likely that we don't have leading comments than we have empty elements here.

return;
}

Expand Down
14 changes: 8 additions & 6 deletions packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -900,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");
}
Expand Down