Browse Source

Perf: speed up file comparison

SukkaW 1 year ago
parent
commit
6f8d515e7c
1 changed files with 15 additions and 8 deletions
  1. 15 8
      Build/lib/rules/base.ts

+ 15 - 8
Build/lib/rules/base.ts

@@ -381,32 +381,39 @@ export abstract class RuleOutput<TPreprocessed = unknown> {
   abstract mitmSgmodule?(): string[] | null;
   abstract mitmSgmodule?(): string[] | null;
 }
 }
 
 
-export async function fileEqual(linesA: string[], source: AsyncIterable<string>): Promise<boolean> {
+export async function fileEqual(linesA: string[], source: AsyncIterable<string> | Iterable<string>): Promise<boolean> {
   if (linesA.length === 0) {
   if (linesA.length === 0) {
     return false;
     return false;
   }
   }
 
 
+  const linesABound = linesA.length - 1;
+
   let index = -1;
   let index = -1;
   for await (const lineB of source) {
   for await (const lineB of source) {
     index++;
     index++;
 
 
-    if (index > linesA.length - 1) {
-      return (index === linesA.length && lineB === '');
+    if (index > linesABound) {
+      return (index === linesA.length && lineB.length === 0);
     }
     }
 
 
     const lineA = linesA[index];
     const lineA = linesA[index];
 
 
-    if (lineA[0] === '#' && lineB[0] === '#') {
+    const firstCharA = lineA.charCodeAt(0);
+    const firstCharB = lineB.charCodeAt(0);
+
+    if (firstCharA === 35 /* # */ && firstCharB === 35 /* # */) {
       continue;
       continue;
     }
     }
     // adguard conf
     // adguard conf
-    if (lineA[0] === '!' && lineB[0] === '!') {
+    if (firstCharA === 33 /* ! */ && firstCharB === 33 /* ! */) {
       continue;
       continue;
     }
     }
+
     if (
     if (
-      lineA[0] === '/'
+      firstCharA === 47 /* / */
+      && firstCharB === 47 /* / */
+
       && lineA[1] === '/'
       && lineA[1] === '/'
-      && lineB[0] === '/'
       && lineB[1] === '/'
       && lineB[1] === '/'
       && lineA[3] === '#'
       && lineA[3] === '#'
       && lineB[3] === '#'
       && lineB[3] === '#'
@@ -420,7 +427,7 @@ export async function fileEqual(linesA: string[], source: AsyncIterable<string>)
   }
   }
 
 
   // The file becomes larger
   // The file becomes larger
-  return !(index < linesA.length - 1);
+  return !(index < linesABound);
 }
 }
 
 
 export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {
 export async function compareAndWriteFile(span: Span, linesA: string[], filePath: string) {