Skip to content

Commit 2eabb36

Browse files
committed
Defer new Intl.Collator() use
1 parent 977f48e commit 2eabb36

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/strings.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
// > When comparing large numbers of strings, such as in sorting large arrays,
66
// > it is better to create an Intl.Collator object and use the function provided
77
// > by its compare() method.
8-
const { compare: localeCompare } = new Intl.Collator()
8+
let _localeCompare
9+
function localeCompare(x, y) {
10+
if (_localeCompare === undefined) {
11+
// Lazily call new Intl.Collator() because in Node it can take 10-14ms.
12+
_localeCompare = new Intl.Collator().compare
13+
}
14+
return _localeCompare(x, y)
15+
}
916

1017
// This regexp is valid as of 2024-08-01.
1118
// https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string

0 commit comments

Comments
 (0)