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);
}
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);
}
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
No description
No description
1 Reply
dys 🐙
dys 🐙4mo ago
Are you sure you're leaking memory & not just blowing the stack with too much data?
Want results from more Discord servers?
Add your server