Perf: improve file comparision

This commit is contained in:
SukkaW 2025-02-23 22:32:11 +08:00
parent 400d892614
commit 3952e27d64

View File

@ -14,6 +14,10 @@ export async function fileEqual(linesA: string[], source: AsyncIterable<string>
const linesABound = linesA.length - 1; const linesABound = linesA.length - 1;
let index = -1; let index = -1;
let aLen = 0;
let bLen = 0;
for await (const lineB of source) { for await (const lineB of source) {
index++; index++;
@ -22,9 +26,11 @@ export async function fileEqual(linesA: string[], source: AsyncIterable<string>
} }
const lineA = linesA[index]; const lineA = linesA[index];
aLen = lineA.length;
bLen = lineB.length;
if (lineA.length === 0) { if (aLen === 0) {
if (lineB.length === 0) { if (bLen === 0) {
// both lines are empty, check next line // both lines are empty, check next line
continue; continue;
} }
@ -32,7 +38,7 @@ export async function fileEqual(linesA: string[], source: AsyncIterable<string>
return false; return false;
} }
// now lineA can not be empty // now lineA can not be empty
if (lineB.length === 0) { if (bLen === 0) {
// lineB is empty but lineA is not // lineB is empty but lineA is not
return false; return false;
} }
@ -63,6 +69,10 @@ export async function fileEqual(linesA: string[], source: AsyncIterable<string>
continue; continue;
} }
if (aLen !== bLen) {
return false;
}
if (lineA !== lineB) { if (lineA !== lineB) {
return false; return false;
} }