How to optimise reading in millions of data | AoC 2023 Day 5 Part 2 | JavaScript

I need a bit of help with my Part 2 solution for day 5. My code below works fine with the sample data provided in the exercise brief, and it returns the correct answer. However, when I replace my input data with the provided puzzle input data I keep getting memory issues related errors, or it just takes forever to run and then it eventually flatlines. I’m using Visual Studio Code and node.js, if that helps.

I’d appreciate any help in optimising my code. ChatGPT wasn’t that helpful with making the code more efficient. Thanks in advance! 🙂

const fs = require('fs');

function readFile(filePath) {
    try {
      return fs.readFileSync(filePath, 'utf-8');
    } catch (error) {
      console.error(`Error reading file: ${error.message}`);
    }
}  

// pull in the .txt file input data
let input = readFile('G:/My Drive/Visual Studio Code/Advent of Code/2023/day05.txt').split(/r?n/);

function isNumberInRange(number, start, end) {
    return number >= start && number <= (start + end);
}

function mappedNumber(number, start, mapStart) {
    return (number - start) + mapStart;
}

function mapSeeds2Location(seeds, location, mapping) {
    for (const originalSeed of seeds) {
        let seed = originalSeed; // we want to keep the original seed number so the mapSeeds2Location function is able to continue through the other seed numbers
    
        for (let i = 0; i < mapping.length; i++) {    
                  
            for (const map of mapping[i]) {
                var maps = map.split(" ").map(Number);
        
                if(isNumberInRange(seed, maps[1], maps[2])) {                    
                    seed = mappedNumber(seed, maps[1], maps[0]); // replace the original seed number with the next mapped number
                    break; // once we have mapped a number, then move onto the next mapping
                };                
            }
    
            // once we get to the final mapping (humidity-to-location) then push the location value to the array
            if(i == mapping.length - 1) {
               location.push(seed);
            }
        };    
    }
}

const arrays = input.reduce((acc, item) => (item === '' ? acc.push([]) : acc[acc.length - 1].push(item), acc), [[]]);

var [seeds, ...mapping] = arrays; // separate the first array from the rest - this is to separate the list of seeds

seeds = seeds[0].split(" ");
seeds.shift();
seeds = seeds.map(Number);
var location = [];

console.log(seeds);

/* Part One
mapSeeds2Location(seeds, location, mapping)
console.log("part one answer = " + Math.min(...location));*/

// Part Two
for (let x = 0; x < seeds.length; x+=2) {
    for (let y=0; y<seeds[x+1]; y++) {
        // for each seed in the range, find the mapped location number
        mapSeeds2Location([y + seeds[x]], location, mapping)
    }
}

let minLocation = location[0];
for (let i = 1; i < location.length; i++) {
    if (location[i] < minLocation) {
        minLocation = location[i];
    }
}

console.log("part two answer = " + minLocation);

// console.log("part two answer = " + Math.min(...location));
// Apparently Math.min() hinders performance if the array is large, thats why I had commented it out