perf(packages/codegen): reduce megamorphic type reads (#27586)

Perf optimization. Continuation of [#27458](https://github.com/oxc-project/oxc/issues/27458).

Avoid reading `type` of a node multiple times where the node can have multiple object shapes. In such a case, loading `.type` is a megamorphic property access, which is expensive, and V8 does not cache the result.
This commit is contained in:
overlookmotel committed 2026-10-12 14:03:08 +00:00
1 parent 1e4c6ee776
commit ff5eae8c5f
2 files changed
+13 -9

No files matched your search

+9 -6
View File
@@ -447,11 +447,12 @@ function printIf(node: ESTree.IfStatement, state: State): void {
writeIdent(state, "else");
if (alternate.type === "BlockStatement") {
const alternateType = alternate.type;
if (alternateType === "BlockStatement") {
write(state, " ", CAT_OTHER);
printBlockStatement(alternate, state);
write(state, "\n", CAT_OTHER);
} else if (alternate.type === "IfStatement") {
} else if (alternateType === "IfStatement") {
write(state, " ", CAT_OTHER);
printIf(alternate, state);
} else {
@@ -634,11 +635,12 @@ function printDoWhileStatement(node: ESTree.DoWhileStatement, state: State): voi
writeWithMap(state, "do", CAT_IDENT, node.start, node.end, node);
const { body } = node;
if (body.type === "BlockStatement") {
const bodyType = body.type;
if (bodyType === "BlockStatement") {
write(state, " ", CAT_OTHER);
printBlockStatement(body, state);
write(state, " ", CAT_OTHER);
} else if (body.type === "EmptyStatement") {
} else if (bodyType === "EmptyStatement") {
writeWithMap(state, ";\n", CAT_OTHER, body.start, body.end, body);
printIndent(state);
} else {
@@ -797,11 +799,12 @@ function forOfHeadStartsWithLet(left: ESTree.Expression): boolean {
* on the current line.
*/
function printBody(stmt: ESTree.Statement, state: State): void {
if (stmt.type === "BlockStatement") {
const stmtType = stmt.type;
if (stmtType === "BlockStatement") {
write(state, " ", CAT_OTHER);
printBlockStatement(stmt, state);
write(state, "\n", CAT_OTHER);
} else if (stmt.type === "EmptyStatement") {
} else if (stmtType === "EmptyStatement") {
write(state, ";\n", CAT_OTHER);
} else {
state.pendingIndentAsSpace = true;
+4 -3
View File
@@ -723,10 +723,11 @@ function printTSTupleElement(node: ESTree.TSTupleElement, state: State): void {
*/
function printTSConditionalType(node: ESTree.TSConditionalType, state: State): void {
const { checkType, extendsType } = node;
const checkTypeType = checkType.type;
const checkWrap =
checkType.type === "TSFunctionType"
|| checkType.type === "TSConstructorType"
|| checkType.type === "TSConditionalType";
checkTypeType === "TSFunctionType"
|| checkTypeType === "TSConstructorType"
|| checkTypeType === "TSConditionalType";
if (checkWrap) write(state, "(", CAT_OTHER);
printTSType(checkType, state);