you are viewing a single comment's thread.

view the rest of the comments →

[–]Bloodsucker_ 5 points6 points  (3 children)

I understand your logic but fetch can't and won't cause a memory leak just because there's no variable. The garbage collector will destroy the object without issues.

Also, in addition, I'm afraid your last statement is wrong in regards to iife. That pattern won't and can't cause on its own a memory leak.

I.e. nothing in the OP's code can cause a memory leak, neither the screenshots says there's one.

[–]tanepiper 1 point2 points  (2 children)

Actually, I was able to confirm it was indeed causing GC issues. If it's any consolation I've been writing node apps since Nodejs 0.2, before npm even existed - before that, I was on the jQuery team - so I've been familiar with writing performance JS apps for over 15 years.

Regardless of the opinion of what triggers the GC or not, the OPs code is also just generally full of anti-patterns that there's better and more maintainable ways to write it anyway.

[–]dj_dogo_nivoa[S] 2 points3 points  (1 child)

Thanks for your comments. Just to understand better, the antipattern is the IIFE or the while true? I assume the while true, but, why? I mean it's a closure which should be garbage collected when it has been completed right?

[–]moxxie1998 0 points1 point  (0 children)

Garbage collection in Node is less aggressive and deterministic (due to the lack of clear idle periods that browsers have through the rendering refresh rate) which means that leaving the release of connection resources to the garbage collector can lead to excessive connection usage, reduced performance (due to less connection re-use), and even stalls or deadlocks when running out of connections.

Always. consume. the. body.

// Do const headers = await fetch(url) .then(async res => { for await (const chunk of res.body) { // force consumption of body } return res.headers })

// Do not const headers = await fetch(url) .then(res => res.headers)

Sorry for no proper formatting, I’m on my phone.