I had thought that asynchronous processing such as reading file is processed on other thread and notify to main thread when reading is finished in other thread.
I tried following.
const fs = require("fs")
console.time(1)
fs.readFile("largefile", x => console.timeEnd(1))
This shows 1500ms.
Secondly I tried following.
const fs = require("fs")
console.time(1)
fs.readFile("largefile", x => console.timeEnd(1))
// block main thread 1sec
const s = Date.now()
while(Date.now() - s < 1000);
It will show 1500ms if asynchronus process is processed on other thread.
However, I got 2500ms.
I tried another one.
const fs = require("fs")
console.time(1)
fs.readFile("largefile", x => console.timeEnd(1))
setInterval(() => {
const s = Date.now()
while(Date.now() - s < 100);
}, 100)
I wait several minute, but there is no message.
Does nodejs process heavy processing on main thread?
Should I use child_process when I need reading and writing too many large files?