Skip to content

This is demo app to learn how worker thread worked in Nodejs

Notifications You must be signed in to change notification settings

nikhilkrdwivedi/nodejs_worker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nodejs Worker Threads

This is demo app to learn how worker thread worked in Nodejs.

Start App

npm i
npm start 

Route Info

1. '/'

This is root route which returns date as data.

2. '/block-execution'

This is blocking route means if you hit this route there is a loop which is going to run 1,000,000 times.

3. '/non-block-execution'

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.

How to test?

Step-1:

Hit '/' and you will get following response:

{
  "data": "2023-06-12T10:37:29.882Z"
}

This is an image

Step-2:

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.

This is an image

This is an image

This is an image

This is an image

Step-3:

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. This is an image

This is an image NOTE: i: 9999999 because we are passing query number with value 9999999. Also, you will get almost instant response when you hit '/' api.

Resources

Worker threads in Nodejs
Express Hello World!

Happy Codings 👨‍💻

About

This is demo app to learn how worker thread worked in Nodejs

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published