Node.js memory leak error

I have this ts code that give me memory leak error every time:
  type Conns = {
    name: string,
    platform: string,
    jobs: job[]
  }[];
  
  type UserType = {
    id: string;
    email: string;
    interval: number;
    next: number;
    connections: Conns;
  };

  const user: UserType = userData;
  const allJobs: Conns[] = [];
  
  const firstTime = user.connections.reduce((min, conn) => {
    const jobDate = Math.min(...conn.jobs.map(job => job.date));
    return jobDate < min ? jobDate : min;
  }, Infinity);
  
  const lastTime = user.next - user.interval;
  
  const cachedJobs = user.connections.map(c => ({
    ...c,
    jobs: c.jobs.sort((a, b) => a.date - b.date)
  }));
  
  const intervalCount = Math.floor((lastTime - firstTime) / user.interval);
  
  for (let i = 1; i <= intervalCount; i++) {
    const intervalTime = lastTime - (user.interval * i * 60 * 1000);
    const jobsInInterval = cachedJobs.map(c => {
      const jobs = c.jobs.filter(j => j.date > intervalTime);
      return {...c, jobs};
    });
    allJobs.push(jobsInInterval);
  }

after some research i found that if i deleted this part the code works fine:
  for (let i = 1; i <= intervalCount; i++) {
    const intervalTime = lastTime - (user.interval * i * 60 * 1000);
    const jobsInInterval = cachedJobs.map(c => {
      const jobs = c.jobs.filter(j => j.date > intervalTime);
      return {...c, jobs};
    });
    allJobs.push(jobsInInterval);
  }

can any one help me fix the error. this code is in next.js app router in the server side
Screenshot_2024-07-18_183744.png
Screenshot_2024-07-18_184130.png
Was this page helpful?