Reputation: 11
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