Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 33 additions & 6 deletions src/lib/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,40 @@ export function percent(received: number, total: number): number {
return Math.min(100, Math.round((received / total) * 100));
}

/** Compare "1.2.10" vs "1.2.9" numerically per segment. Good enough for mod versions. */
/**
* Compare "1.2.10" vs "1.2.9" numerically per segment.
* Single-pass numerical segment parsing eliminates string split and array allocations during list operations.
*/
export function compareVersions(a: string, b: string): number {
const pa = a.split(".").map((s) => parseInt(s, 10) || 0);
const pb = b.split(".").map((s) => parseInt(s, 10) || 0);
for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
const diff = (pa[i] ?? 0) - (pb[i] ?? 0);
if (diff !== 0) return diff;
let i = 0;
let j = 0;
const lenA = a.length;
const lenB = b.length;

while (i < lenA || j < lenB) {
let numA = 0;
while (i < lenA && a.charCodeAt(i) !== 46 /* '.' */) {
const code = a.charCodeAt(i);
if (code >= 48 && code <= 57) {
numA = numA * 10 + (code - 48);
}
i++;
}
if (i < lenA) i++;

let numB = 0;
while (j < lenB && b.charCodeAt(j) !== 46 /* '.' */) {
const code = b.charCodeAt(j);
if (code >= 48 && code <= 57) {
numB = numB * 10 + (code - 48);
}
j++;
}
if (j < lenB) j++;

if (numA !== numB) {
return numA - numB;
}
}
return 0;
}
7 changes: 5 additions & 2 deletions src/pages/InstalledPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ import Spinner from "../components/ui/Spinner";
import { useConfirm } from "../components/ui/useConfirm";
import type { InstalledMod, UpdatesReport } from "../types";

/** Reuse a single Intl.Collator instance across list sorts to avoid repeated allocations. */
const modNameCollator = new Intl.Collator(undefined, { sensitivity: "base" });

/** Prevent interactive controls inside the row from toggling expansion. */
function stopRow(e: React.MouseEvent) {
e.stopPropagation();
Expand Down Expand Up @@ -448,8 +451,8 @@ export default function InstalledPage() {
}
}
const cmp = (a: InstalledMod, b: InstalledMod) =>
a.name.localeCompare(b.name, undefined, { sensitivity: "base" }) ||
a.name.localeCompare(b.name);
modNameCollator.compare(a.name, b.name) ||
(a.name < b.name ? -1 : a.name > b.name ? 1 : 0);
enabled.sort(cmp);
disabled.sort(cmp);
return { enabledMods: enabled, disabledMods: disabled };
Expand Down
Loading