Skip to content

Commit c9f6107

Browse files
committed
perf: use Intl.Collator for compare
1 parent 2fb1afb commit c9f6107

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/core/tree.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export interface TreeNodeOptions extends ResolvedOptions {
1212
treeNodeOptions?: TreeNodeValueOptions
1313
}
1414

15+
const stringCompare = new Intl.Collator('en', {}).compare
16+
1517
export class TreeNode {
1618
/**
1719
* value of the node
@@ -142,24 +144,26 @@ export class TreeNode {
142144
}
143145

144146
/**
145-
* Comparator function for sorting TreeNodes by path.
147+
* Comparator function for sorting TreeNodes.
148+
*
149+
* @internal
146150
*/
147-
static compareByPath(a: TreeNode, b: TreeNode): number {
148-
return a.path.localeCompare(b.path)
151+
static compare(a: TreeNode, b: TreeNode): number {
152+
return stringCompare(a.path, b.path)
149153
}
150154

151155
/**
152156
* Get the children of this node sorted by their path.
153157
*/
154158
getSortedChildren(): TreeNode[] {
155-
return Array.from(this.children.values()).sort(TreeNode.compareByPath)
159+
return Array.from(this.children.values()).sort(TreeNode.compare)
156160
}
157161

158162
/**
159163
* Calls {@link getChildrenDeep} and sorts the result by path in the end.
160164
*/
161165
getChildrenDeepSorted(): TreeNode[] {
162-
return Array.from(this.getChildrenDeep()).sort(TreeNode.compareByPath)
166+
return Array.from(this.getChildrenDeep()).sort(TreeNode.compare)
163167
}
164168

165169
/**

0 commit comments

Comments
 (0)