This is demo app to learn how worker thread worked in Nodejs.
npm i
npm start
This is root route which returns date as data.
This is blocking route means if you hit this route there is a loop which is going to run 1,000,000 times.
This is non-blocking route means if you hit this route there is a loop which is going to run 1,000,000 times, but this loop will not run in main thread.
Hit '/' and you will get following response:
{
"data": "2023-06-12T10:37:29.882Z"
}
Hit '/block-execution' and you will get following response:
{
"data": "2023-06-12T10:07:04.432Z",
"i": 1000000
}
But this response will take sometime, in my case it took 39.76 seconds. When I hit '/' api it didn't return response. But after '/block-execution' api fullfilled response '/' returned response after 40.56 seconds.
Hit '/non-block-execution' and you will get following response:
{
"data": "2023-06-12T10:07:04.432Z",
"i": 9999999
}
here response time is 9.50 seconds because we offload excution to other worker thread.
NOTE: i: 9999999 because we are passing query number with value 9999999.
Also, you will get almost instant response when you hit '/' api.
Worker threads in Nodejs
Express Hello World!
Happy Codings 👨💻