From 9e2fc16c32e710ff342d1932ef4668bbcfb22d86 Mon Sep 17 00:00:00 2001 From: Yuya Tanaka Date: Wed, 13 Oct 2021 08:07:21 +0900 Subject: [PATCH] Improve perf of unions with many primitives (#45220) --- src/compiler/checker.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 3d54f2e9db5..c11ce26d883 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -22652,8 +22652,9 @@ namespace ts { // constituent types keyed by the literal types of the property by that name in each constituent type. function getKeyPropertyName(unionType: UnionType): __String | undefined { const types = unionType.types; - // We only construct maps for large unions with non-primitive constituents. - if (types.length < 10 || getObjectFlags(unionType) & ObjectFlags.PrimitiveUnion) { + // We only construct maps for unions with many non-primitive constituents. + if (types.length < 10 || getObjectFlags(unionType) & ObjectFlags.PrimitiveUnion || + countWhere(types, t => !!(t.flags & (TypeFlags.Object | TypeFlags.InstantiableNonPrimitive))) < 10) { return undefined; } if (unionType.keyPropertyName === undefined) {