Skip to content

Commit 660a061

Browse files
committed
Improve perf of unions with many primitives
1 parent d1d65cb commit 660a061

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/compiler/checker.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -22168,8 +22168,17 @@ namespace ts {
2216822168
// constituent types keyed by the literal types of the property by that name in each constituent type.
2216922169
function getKeyPropertyName(unionType: UnionType): __String | undefined {
2217022170
const types = unionType.types;
22171-
// We only construct maps for large unions with non-primitive constituents.
22172-
if (types.length < 10 || getObjectFlags(unionType) & ObjectFlags.PrimitiveUnion) {
22171+
// We only construct maps for unions with many non-primitive constituents.
22172+
let nonPrimitiveCount = 0;
22173+
const isLargeNonPrimitives = types.length >= 10 && !(getObjectFlags(unionType) & ObjectFlags.PrimitiveUnion) &&
22174+
forEach(types, t => {
22175+
if (t.flags & (TypeFlags.Primitive | TypeFlags.AnyOrUnknown | TypeFlags.Never)) {
22176+
return
22177+
}
22178+
nonPrimitiveCount++;
22179+
return nonPrimitiveCount >= 10;
22180+
});
22181+
if (!isLargeNonPrimitives) {
2217322182
return undefined;
2217422183
}
2217522184
if (unionType.keyPropertyName === undefined) {

0 commit comments

Comments
 (0)