Skip to content

Commit

Permalink
turn array into object to save memory
Browse files Browse the repository at this point in the history
  • Loading branch information
justindhillon committed Feb 13, 2024
1 parent d7d4491 commit 1193b46
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { checkLink } from "./checkLink";
import fs from 'fs';

let PROCESSES: string[][] = [];
let QUEUE:Record<number, string[]> = {};
let PROCESS: number = 0;
let CURRENTPROCESS: number = 0;
let RUNNINGPROCESSES: number = 0;
const MAXPROCESSES: number = 100;

async function runProcess(callback: any) {
if (MAXPROCESSES <= RUNNINGPROCESSES || PROCESSES.length === CURRENTPROCESS) return;
if (MAXPROCESSES <= RUNNINGPROCESSES || !QUEUE[PROCESS-1]) return;

RUNNINGPROCESSES++;
const [link, path] = PROCESSES[CURRENTPROCESS]!;
delete PROCESSES[CURRENTPROCESS];
const [link, path] = QUEUE[CURRENTPROCESS]!;
delete QUEUE[CURRENTPROCESS];
CURRENTPROCESS++;

try {
Expand All @@ -24,10 +25,11 @@ async function runProcess(callback: any) {
runProcess(callback);
}

export default async function linkInspector(arg: string, callback: any, path: string = '') {
export default async function linkInspector(arg: string, callback: any, path: string = '') {
try { // If arg is a link
new URL(arg);
PROCESSES.push([arg, path]);
QUEUE[PROCESS] = [arg, path];
PROCESS++;
runProcess(callback);
return;
} catch {}
Expand Down Expand Up @@ -66,7 +68,8 @@ export default async function linkInspector(arg: string, callback: any, path: st
for (const link of links) {
try { // Runs link inspector on each link
new URL(link);
PROCESSES.push([link, pathAfterDirectory]);
QUEUE[PROCESS] = [link, pathAfterDirectory];
PROCESS++;
runProcess(callback);
} catch {}
}
Expand Down

0 comments on commit 1193b46

Please sign in to comment.