Faster checkReservedWord (#13386)

* perf: faster parser scope check

* perf: early return for identifier length > 10

* perf: early return for normal identifier names

* chore: add benchmark

* Update packages/babel-parser/src/parser/expression.js
This commit is contained in:
Huáng Jùnliàng
2021-06-01 08:42:23 -04:00
committed by GitHub
parent ae3f5d905a
commit cbad50ac1d
5 changed files with 154 additions and 35 deletions

View File

@@ -21,3 +21,66 @@ export const keywordRelationalOperator = /^in(stanceof)?$/;
export function isIteratorStart(current: number, next: number): boolean {
return current === charCodes.atSign && next === charCodes.atSign;
}
// This is the comprehensive set of JavaScript reserved words
// If a word is in this set, it could be a reserved word,
// depending on sourceType/strictMode/binding info. In other words
// if a word is not in this set, it is not a reserved word under
// any circumstance.
const reservedWordLikeSet = new Set([
"break",
"case",
"catch",
"continue",
"debugger",
"default",
"do",
"else",
"finally",
"for",
"function",
"if",
"return",
"switch",
"throw",
"try",
"var",
"const",
"while",
"with",
"new",
"this",
"super",
"class",
"extends",
"export",
"import",
"null",
"true",
"false",
"in",
"instanceof",
"typeof",
"void",
"delete",
// strict
"implements",
"interface",
"let",
"package",
"private",
"protected",
"public",
"static",
"yield",
// strictBind
"eval",
"arguments",
// reservedWorkLike
"enum",
"await",
]);
export function canBeReservedWord(word: string): boolean {
return reservedWordLikeSet.has(word);
}

View File

@@ -49,22 +49,26 @@ export default class ScopeHandler<IScope: Scope = Scope> {
}
get inFunction() {
return (this.currentVarScope().flags & SCOPE_FUNCTION) > 0;
return (this.currentVarScopeFlags() & SCOPE_FUNCTION) > 0;
}
get allowSuper() {
return (this.currentThisScope().flags & SCOPE_SUPER) > 0;
return (this.currentThisScopeFlags() & SCOPE_SUPER) > 0;
}
get allowDirectSuper() {
return (this.currentThisScope().flags & SCOPE_DIRECT_SUPER) > 0;
return (this.currentThisScopeFlags() & SCOPE_DIRECT_SUPER) > 0;
}
get inClass() {
return (this.currentThisScope().flags & SCOPE_CLASS) > 0;
return (this.currentThisScopeFlags() & SCOPE_CLASS) > 0;
}
get inClassAndNotInNonArrowFunction() {
const flags = this.currentThisScopeFlags();
return (flags & SCOPE_CLASS) > 0 && (flags & SCOPE_FUNCTION) === 0;
}
get inStaticBlock() {
return (this.currentThisScope().flags & SCOPE_STATIC_BLOCK) > 0;
return (this.currentThisScopeFlags() & SCOPE_STATIC_BLOCK) > 0;
}
get inNonArrowFunction() {
return (this.currentThisScope().flags & SCOPE_FUNCTION) > 0;
return (this.currentThisScopeFlags() & SCOPE_FUNCTION) > 0;
}
get treatFunctionsAsVar() {
return this.treatFunctionsAsVarInScope(this.currentScope());
@@ -189,25 +193,22 @@ export default class ScopeHandler<IScope: Scope = Scope> {
}
// $FlowIgnore
currentVarScope(): IScope {
currentVarScopeFlags(): ScopeFlags {
for (let i = this.scopeStack.length - 1; ; i--) {
const scope = this.scopeStack[i];
if (scope.flags & SCOPE_VAR) {
return scope;
const { flags } = this.scopeStack[i];
if (flags & SCOPE_VAR) {
return flags;
}
}
}
// Could be useful for `arguments`, `this`, `new.target`, `super()`, `super.property`, and `super[property]`.
// $FlowIgnore
currentThisScope(): IScope {
currentThisScopeFlags(): ScopeFlags {
for (let i = this.scopeStack.length - 1; ; i--) {
const scope = this.scopeStack[i];
if (
(scope.flags & SCOPE_VAR || scope.flags & SCOPE_CLASS) &&
!(scope.flags & SCOPE_ARROW)
) {
return scope;
const { flags } = this.scopeStack[i];
if (flags & (SCOPE_VAR | SCOPE_CLASS) && !(flags & SCOPE_ARROW)) {
return flags;
}
}
}