mirror of
https://github.com/aphrody-labs/oxc.git
synced 2026-10-17 12:56:09 +00:00
perf(packages/codegen): reduce branches in printObjectProperty (#27588)
Perf optimization. Move a branch which depends on `!computed` into inside an existing branch which already has that check, removing the 2nd branch on that condition.
This commit is contained in:
1 parent
b4c8c04b91
commit
8d4c633137
1 file changed
+20
-20
@@ -523,27 +523,27 @@ function printObjectProperty(node: ESTree.ObjectPropertyKind, state: State): voi
|
||||
// the expression printer what it is.
|
||||
// A `__proto__` shorthand leaves this null, since its flag says nothing about the value it carries.
|
||||
let shorthandIdentifier: ESTree.IdentifierReference | null = null;
|
||||
if (!node.computed && key.type === "Identifier") {
|
||||
if (key.name === "__proto__") {
|
||||
shorthand = node.shorthand;
|
||||
} else {
|
||||
const inner = withoutParens(value);
|
||||
if (inner.type === "Identifier" && key.name === inner.name) {
|
||||
shorthand = true;
|
||||
shorthandIdentifier = inner;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let { computed } = node;
|
||||
// `{ -1: 0 }` and `{ 1/0: 0 }` must print as `{ [-1]: 0 }` / `{ [1 / 0]: 0 }`
|
||||
if (
|
||||
!computed
|
||||
&& key.type === "Literal"
|
||||
&& typeof key.value === "number"
|
||||
&& (key.value < 0 || Object.is(key.value, -0) || !Number.isFinite(key.value))
|
||||
) {
|
||||
computed = true;
|
||||
if (!computed) {
|
||||
if (key.type === "Identifier") {
|
||||
const keyName = key.name;
|
||||
if (keyName === "__proto__") {
|
||||
shorthand = node.shorthand;
|
||||
} else {
|
||||
const inner = withoutParens(value);
|
||||
if (inner.type === "Identifier" && inner.name === keyName) {
|
||||
shorthand = true;
|
||||
shorthandIdentifier = inner;
|
||||
}
|
||||
}
|
||||
} else if (
|
||||
key.type === "Literal"
|
||||
&& typeof key.value === "number"
|
||||
&& (key.value < 0 || Object.is(key.value, -0) || !Number.isFinite(key.value))
|
||||
) {
|
||||
// `{ -1: 0 }` and `{ 1/0: 0 }` must print as `{ [-1]: 0 }` / `{ [1 / 0]: 0 }`
|
||||
computed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (shorthand) {
|
||||
|
||||
Reference in new issue
Block a user