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

perf: minimize identifier lookahead when parsing let #13328

Merged
merged 2 commits into from May 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
@@ -0,0 +1,22 @@
import Benchmark from "benchmark";
import baseline from "@babel-baseline/parser";
import current from "../../lib/index.js";
import { report } from "../util.mjs";

const suite = new Benchmark.Suite();
function createInput(length) {
return "{ let foecnatsni };".repeat(length);
}
function benchCases(name, implementation, options) {
for (const length of [64, 128, 256, 512]) {
const input = createInput(length);
suite.add(`${name} ${length} let and length-10 binding identifiers`, () => {
implementation.parse(input, options);
});
}
}

benchCases("baseline", baseline);
benchCases("current", current);

suite.on("cycle", report).run();
24 changes: 14 additions & 10 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -4,11 +4,7 @@ import * as N from "../types";
import { types as tt, type TokenType } from "../tokenizer/types";
import ExpressionParser from "./expression";
import { Errors, SourceTypeModuleErrors } from "./error";
import {
isIdentifierChar,
isIdentifierStart,
keywordRelationalOperator,
} from "../util/identifier";
import { isIdentifierChar, isIdentifierStart } from "../util/identifier";
import { lineBreak } from "../util/whitespace";
import * as charCodes from "charcodes";
import {
Expand Down Expand Up @@ -49,6 +45,8 @@ const FUNC_NO_FLAGS = 0b000,

const loneSurrogate = /[\uD800-\uDFFF]/u;

const keywordRelationalOperator = /in(?:stanceof)?/y;

/**
* Convert tt.privateName to tt.hash + tt.name for backward Babel 7 compat.
* For performance reasons this routine mutates `tokens`, it is okay
Expand Down Expand Up @@ -200,12 +198,18 @@ export default class StatementParser extends ExpressionParser {
if (nextCh === charCodes.leftCurlyBrace) return true;

if (isIdentifierStart(nextCh)) {
let pos = next + 1;
while (isIdentifierChar(this.input.charCodeAt(pos))) {
++pos;
keywordRelationalOperator.lastIndex = next;
const matched = keywordRelationalOperator.exec(this.input);
if (
matched !== null &&
// We have seen `in` or `instanceof` so far, now check if the identfier
// ends here
!isIdentifierChar(this.input.charCodeAt(next + matched[0].length))
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
) {
return false;
} else {
return true;
}
const ident = this.input.slice(next, pos);
if (!keywordRelationalOperator.test(ident)) return true;
}
return false;
}
Expand Down