x072ux73hx69
x072ux73hx69

Reputation: 11

Why, when I use the drain event my memory usage is High compared to when I don't use the drain event, while writing to file using stream in NodeJS?

With Drain Event

const fs = require("node:fs/promises");

( async ()=>{

console.time("writeMany");
const fd = await fs.open("./text.txt","w");
const stream = fd.createWriteStream();
let i = 0;

const writeMany = () => {
    while( i < 1000000 ){
     const buff = Buffer.from(`${i} `,"utf-8");

     if(i === 999999) return stream.end(buff);

     if(!stream.write(buff)) break;

     i++;   
   } 
  } 
  writeMany();
  stream.on("drain",()=>{
             writeMany();
   });

  stream.on("finish",() => {
    console.timeEnd("writeMany");
    fd.close();   
  });
})();

Without Drain event

(async () =>{

    const fd = await fs.open("text.txt","w");
    const stream = fd.createWriteStream();
  console.time("writeMany");
    for(let i=0 ; i<100000 ; i++){
        const buffer = Buffer.from(`${i}`,"utf-8");
        stream.write(buffer);
    }
    console.timeEnd("writeMany");
  
  } )();

The first function where i used Drain Event the memory usage should be less compare to the second function but its actually opposite of it. First function is taking up to 200MB.

MY UNDERSTANDING

According to my understanding if I am not using drain event, Once the stream buffer is filled , Remaining data will be pushed to the memory.

As i am recursively calling stream.write() even after stream buffer is occupied to its fullest it will fail and return false and hence until Drain Event is executed asynchronously, it will fill up that data into ram and not in the stream Which will result in High Ram usage.

But even after using Drain event, its still taking like 200MB on my ram , Plus in second function without even using Drain Event its consuming Low memory why??

Upvotes: 0

Views: 21

Answers (0)

Related Questions