Perf: repalce String#localeCompare w/ simple ASCII-only compare

This commit is contained in:
SukkaW
2024-11-21 21:51:05 +08:00
parent 200da7a2be
commit c6f3a67711
5 changed files with 28 additions and 6 deletions

View File

@@ -21,6 +21,26 @@ export function fastStringArrayJoin(arr: string[], sep: string) {
return result;
}
export function fastStringCompare(a: string, b: string) {
const lenA = a.length;
const lenB = b.length;
const minLen = lenA < lenB ? lenA : lenB;
for (let i = 0; i < minLen; ++i) {
const ca = a.charCodeAt(i);
const cb = b.charCodeAt(i);
if (ca > cb) return 1;
if (ca < cb) return -1;
}
if (lenA === lenB) {
return 0;
}
return lenA > lenB ? 1 : -1;
};
interface Write {
(
destination: string,