From 90bbf223dbf95f641b14e276718bd201a142f42f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 4 Oct 2026 11:31:41 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20optimize=20compareVersions?= =?UTF-8?q?=20with=20allocation-free=20parsing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Eliminate array .split() and .map() allocations in compareVersions by using a single-pass numeric segment parsing loop. Co-authored-by: TheDragonSoft <75906592+TheDragonSoft@users.noreply.github.com> --- src/lib/format.ts | 38 +++++++++++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/src/lib/format.ts b/src/lib/format.ts index 345259d..2339644 100644 --- a/src/lib/format.ts +++ b/src/lib/format.ts @@ -19,13 +19,41 @@ 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 numeric segment parsing eliminates array allocations (.split and .map) + * during array sort comparators. + */ 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); + const lenA = a.length; + const lenB = b.length; + let i = 0; + let j = 0; + + while (i < lenA || j < lenB) { + let valA = 0; + while (i < lenA && a[i] !== ".") { + const code = a.charCodeAt(i); + if (code >= 48 && code <= 57) { + valA = valA * 10 + (code - 48); + } + i++; + } + if (i < lenA) i++; // skip '.' + + let valB = 0; + while (j < lenB && b[j] !== ".") { + const code = b.charCodeAt(j); + if (code >= 48 && code <= 57) { + valB = valB * 10 + (code - 48); + } + j++; + } + if (j < lenB) j++; // skip '.' + + const diff = valA - valB; if (diff !== 0) return diff; } + return 0; }