Skip to content

Commit

Permalink
Improve performance of Debug.format functions (#49487)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakebailey committed Jun 10, 2022
1 parent 806a710 commit 678afe8
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/compiler/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,19 +301,19 @@ namespace ts {
return members.length > 0 && members[0][0] === 0 ? members[0][1] : "0";
}
if (isFlags) {
let result = "";
const result: string[] = [];
let remainingFlags = value;
for (const [enumValue, enumName] of members) {
if (enumValue > value) {
break;
}
if (enumValue !== 0 && enumValue & value) {
result = `${result}${result ? "|" : ""}${enumName}`;
result.push(enumName);
remainingFlags &= ~enumValue;
}
}
if (remainingFlags === 0) {
return result;
return result.join("|");
}
}
else {
Expand All @@ -326,7 +326,17 @@ namespace ts {
return value.toString();
}

const enumMemberCache = new Map<any, SortedReadonlyArray<[number, string]>>();

function getEnumMembers(enumObject: any) {
// Assuming enum objects do not change at runtime, we can cache the enum members list
// to reuse later. This saves us from reconstructing this each and every time we call
// a formatting function (which can be expensive for large enums like SyntaxKind).
const existing = enumMemberCache.get(enumObject);
if (existing) {
return existing;
}

const result: [number, string][] = [];
for (const name in enumObject) {
const value = enumObject[name];
Expand All @@ -335,7 +345,9 @@ namespace ts {
}
}

return stableSort<[number, string]>(result, (x, y) => compareValues(x[0], y[0]));
const sorted = stableSort<[number, string]>(result, (x, y) => compareValues(x[0], y[0]));
enumMemberCache.set(enumObject, sorted);
return sorted;
}

export function formatSyntaxKind(kind: SyntaxKind | undefined): string {
Expand Down

0 comments on commit 678afe8

Please sign in to comment.