Skip to content

Commit

Permalink
perf: Make VISITOR_KEYS etc. faster to access (babel#16918)
Browse files Browse the repository at this point in the history
* perf

* review

* review
  • Loading branch information
liuxingbaoyu authored Oct 20, 2024
1 parent 831396d commit d20b314
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 36 deletions.
11 changes: 8 additions & 3 deletions packages/babel-parser/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,21 @@ export const defaultOptions: OptionsWithDefaults = {
// Interpret and default an options object

export function getOptions(opts?: Options | null): OptionsWithDefaults {
// https://github.com/babel/babel/pull/16918
// `options` is accessed frequently, please make sure it is a fast object.
// `%ToFastProperties` can make it a fast object, but the performance is the same as the slow object.
const options: any = { ...defaultOptions };

if (opts == null) {
return { ...defaultOptions };
return options;
}
if (opts.annexB != null && opts.annexB !== false) {
throw new Error("The `annexB` option can only be set to `false`.");
}

const options: any = {};
for (const key of Object.keys(defaultOptions) as (keyof Options)[]) {
options[key] = opts[key] ?? defaultOptions[key];
if (opts[key] != null) options[key] = opts[key];
}

return options;
}
3 changes: 1 addition & 2 deletions packages/babel-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
},
"dependencies": {
"@babel/helper-string-parser": "workspace:^",
"@babel/helper-validator-identifier": "workspace:^",
"to-fast-properties": "condition:BABEL_8_BREAKING ? ^3.0.0 : ^2.0.0"
"@babel/helper-validator-identifier": "workspace:^"
},
"devDependencies": {
"@babel/generator": "workspace:^",
Expand Down
37 changes: 19 additions & 18 deletions packages/babel-types/src/definitions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import toFastProperties from "to-fast-properties";
import "./core.ts";
import "./flow.ts";
import "./jsx.ts";
Expand Down Expand Up @@ -29,15 +28,17 @@ import { DEPRECATED_ALIASES } from "./deprecated-aliases.ts";
});

// We do this here, because at this point the visitor keys should be ready and setup
toFastProperties(VISITOR_KEYS);
toFastProperties(ALIAS_KEYS);
toFastProperties(FLIPPED_ALIAS_KEYS);
toFastProperties(NODE_FIELDS);
toFastProperties(BUILDER_KEYS);
toFastProperties(DEPRECATED_KEYS);
// `%ToFastProperties` no longer improves performance, so we use another approach.
const _VISITOR_KEYS = { ...VISITOR_KEYS };
const _ALIAS_KEYS = { ...ALIAS_KEYS };
const _FLIPPED_ALIAS_KEYS = { ...FLIPPED_ALIAS_KEYS };
const _NODE_FIELDS = { ...NODE_FIELDS };
const _BUILDER_KEYS = { ...BUILDER_KEYS };
const _DEPRECATED_KEYS = { ...DEPRECATED_KEYS };
const _NODE_PARENT_VALIDATIONS = { ...NODE_PARENT_VALIDATIONS };

toFastProperties(PLACEHOLDERS_ALIAS);
toFastProperties(PLACEHOLDERS_FLIPPED_ALIAS);
const _PLACEHOLDERS_ALIAS = { ...PLACEHOLDERS_ALIAS };
const _PLACEHOLDERS_FLIPPED_ALIAS = { ...PLACEHOLDERS_FLIPPED_ALIAS };

const TYPES: Array<string> = [].concat(
Object.keys(VISITOR_KEYS),
Expand All @@ -46,17 +47,17 @@ const TYPES: Array<string> = [].concat(
);

export {
VISITOR_KEYS,
ALIAS_KEYS,
FLIPPED_ALIAS_KEYS,
NODE_FIELDS,
BUILDER_KEYS,
_VISITOR_KEYS as VISITOR_KEYS,
_ALIAS_KEYS as ALIAS_KEYS,
_FLIPPED_ALIAS_KEYS as FLIPPED_ALIAS_KEYS,
_NODE_FIELDS as NODE_FIELDS,
_BUILDER_KEYS as BUILDER_KEYS,
DEPRECATED_ALIASES,
DEPRECATED_KEYS,
NODE_PARENT_VALIDATIONS,
_DEPRECATED_KEYS as DEPRECATED_KEYS,
_NODE_PARENT_VALIDATIONS as NODE_PARENT_VALIDATIONS,
PLACEHOLDERS,
PLACEHOLDERS_ALIAS,
PLACEHOLDERS_FLIPPED_ALIAS,
_PLACEHOLDERS_ALIAS as PLACEHOLDERS_ALIAS,
_PLACEHOLDERS_FLIPPED_ALIAS as PLACEHOLDERS_FLIPPED_ALIAS,
TYPES,
};

Expand Down
15 changes: 2 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4257,7 +4257,6 @@ __metadata:
"@babel/helper-validator-identifier": "workspace:^"
"@babel/parser": "workspace:^"
glob: "npm:^7.2.0"
to-fast-properties: "condition:BABEL_8_BREAKING ? ^3.0.0 : ^2.0.0"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -16234,30 +16233,20 @@ __metadata:
languageName: node
linkType: hard

"to-fast-properties-BABEL_8_BREAKING-false@npm:to-fast-properties@^2.0.0, to-fast-properties@npm:^2.0.0":
"to-fast-properties@npm:^2.0.0":
version: 2.0.0
resolution: "to-fast-properties@npm:2.0.0"
checksum: 10/be2de62fe58ead94e3e592680052683b1ec986c72d589e7b21e5697f8744cdbf48c266fa72f6c15932894c10187b5f54573a3bcf7da0bfd964d5caf23d436168
languageName: node
linkType: hard

"to-fast-properties-BABEL_8_BREAKING-true@npm:to-fast-properties@^3.0.0, to-fast-properties@npm:^3.0.0":
"to-fast-properties@npm:^3.0.0":
version: 3.0.1
resolution: "to-fast-properties@npm:3.0.1"
checksum: 10/66b79f2c0d420d116a4eeb781c87c8c6b2de426a15f16118759e706aa1111833b0c578355a1108514ada966c08f4dba0cb7994aca025d80a535a139f7a631e22
languageName: node
linkType: hard

"to-fast-properties@condition:BABEL_8_BREAKING ? ^3.0.0 : ^2.0.0":
version: 0.0.0-condition-05bba4
resolution: "to-fast-properties@condition:BABEL_8_BREAKING?^3.0.0:^2.0.0#05bba4"
dependencies:
to-fast-properties-BABEL_8_BREAKING-false: "npm:to-fast-properties@^2.0.0"
to-fast-properties-BABEL_8_BREAKING-true: "npm:to-fast-properties@^3.0.0"
checksum: 10/9baff3822ec19add946c04335d91f8b3cc387c25e7879e8ba5c3a6879a9371bce46866e42b90058b7d89dd45ffa60c433086affb347df19b9a45eac201f9385a
languageName: node
linkType: hard

"to-object-path@npm:^0.3.0":
version: 0.3.0
resolution: "to-object-path@npm:0.3.0"
Expand Down

0 comments on commit d20b314

Please sign in to comment.