Skip to content

Commit d36cd9b

Browse files
authored
Merge pull request #11683 from Microsoft/useCollator_toImprovcePerformance
Remove localeFix function and use collator object when we can
2 parents 4759ade + 2bb4abc commit d36cd9b

File tree

2 files changed

+10
-25
lines changed

2 files changed

+10
-25
lines changed

src/compiler/core.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ namespace ts {
2424
}
2525

2626
// More efficient to create a collator once and use its `compare` than to call `a.localeCompare(b)` many times.
27-
export const collator: { compare(a: string, b: string): number } = typeof Intl === "object" && typeof Intl.Collator === "function" ? new Intl.Collator() : undefined;
27+
export const collator: { compare(a: string, b: string): number } = typeof Intl === "object" && typeof Intl.Collator === "function" ? new Intl.Collator(/*locales*/ undefined, { usage: "sort", sensitivity: "accent" }) : undefined;
28+
// Intl is missing in Safari, and node 0.10 treats "a" as greater than "B".
29+
export const localeCompareIsCorrect = ts.collator && ts.collator.compare("a", "B") < 0;
2830

2931
/** Create a MapLike with good performance. */
3032
function createDictionaryObject<T>(): MapLike<T> {
@@ -1247,9 +1249,12 @@ namespace ts {
12471249
if (a === undefined) return Comparison.LessThan;
12481250
if (b === undefined) return Comparison.GreaterThan;
12491251
if (ignoreCase) {
1250-
if (collator && String.prototype.localeCompare) {
1251-
// accent means a ≠ b, a ≠ á, a = A
1252-
const result = a.localeCompare(b, /*locales*/ undefined, { usage: "sort", sensitivity: "accent" });
1252+
// Checking if "collator exists indicates that Intl is available.
1253+
// We still have to check if "collator.compare" is correct. If it is not, use "String.localeComapre"
1254+
if (collator) {
1255+
const result = localeCompareIsCorrect ?
1256+
collator.compare(a, b) :
1257+
a.localeCompare(b, /*locales*/ undefined, { usage: "sort", sensitivity: "accent" }); // accent means a ≠ b, a ≠ á, a = A
12531258
return result < 0 ? Comparison.LessThan : result > 0 ? Comparison.GreaterThan : Comparison.EqualTo;
12541259
}
12551260

src/services/navigationBar.ts

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -322,34 +322,14 @@ namespace ts.NavigationBar {
322322
function compareChildren(child1: NavigationBarNode, child2: NavigationBarNode): number {
323323
const name1 = tryGetName(child1.node), name2 = tryGetName(child2.node);
324324
if (name1 && name2) {
325-
const cmp = localeCompareFix(name1, name2);
325+
const cmp = ts.compareStringsCaseInsensitive(name1, name2);
326326
return cmp !== 0 ? cmp : navigationBarNodeKind(child1) - navigationBarNodeKind(child2);
327327
}
328328
else {
329329
return name1 ? 1 : name2 ? -1 : navigationBarNodeKind(child1) - navigationBarNodeKind(child2);
330330
}
331331
}
332332

333-
// Intl is missing in Safari, and node 0.10 treats "a" as greater than "B".
334-
const localeCompareIsCorrect = ts.collator && ts.collator.compare("a", "B") < 0;
335-
const localeCompareFix: (a: string, b: string) => number = localeCompareIsCorrect ? collator.compare : function(a, b) {
336-
// This isn't perfect, but it passes all of our tests.
337-
for (let i = 0; i < Math.min(a.length, b.length); i++) {
338-
const chA = a.charAt(i), chB = b.charAt(i);
339-
if (chA === "\"" && chB === "'") {
340-
return 1;
341-
}
342-
if (chA === "'" && chB === "\"") {
343-
return -1;
344-
}
345-
const cmp = ts.compareStrings(chA.toLocaleLowerCase(), chB.toLocaleLowerCase());
346-
if (cmp !== 0) {
347-
return cmp;
348-
}
349-
}
350-
return a.length - b.length;
351-
};
352-
353333
/**
354334
* This differs from getItemName because this is just used for sorting.
355335
* We only sort nodes by name that have a more-or-less "direct" name, as opposed to `new()` and the like.

0 commit comments

Comments
 (0)