Showing posts with label single-thread. Show all posts
Showing posts with label single-thread. Show all posts

Saturday, November 19, 2016

Web worker for separate thread in JavaScript

What Web worker is ?


JavaScript is a single-threaded programming language. It would be executed in a single thread only, this is the reason when you use heavy JavaScript code for your application, the UI of application would be blocked because the heavy script uses all CPU with main/single thread.


In a single thread, all the script is executed one by one, If the particular code uses the extensive CPU which means other code can not be executed meanwhile.

As a result, JavaScript application is not able to give the response to user interaction, so It’s pretty bad experience to end user.

Web-worker shines over here, what basically it does is, provide the facility of multi-threading for JavaScript application. So the application can use the heavy code in another thread by using web worker and make free the main thread to respond the user input.


For example, if you run large loop in your JavaScript code, which uses 95% CPU, now suppose user click on a button for any task, then user interface in the browser is not responded, in this case, we can place that heavy code in worker as a separate thread.

So that ”95% CPU consuming code” would be executed in the background with a different thread, and the main thread is available for User Interface.

How to use Web Worker ?

To add the web worker on your page, you could do something like.

 var anyWorker  = new Worker('anyJsFile.js'); 

anyJsFile.js is worker file treated as separate thread, now the worker object is available with anyWorker object,

By using postMessage() method with worker object we can send the data to worker, like

Main.js
// the data should be in object format
var car = {
  model : 'GLA-200',
 color : 'White',
 price : null
}
// the data would pass to the worker
anyWorker.postMessage(car); 
This above code sends the car object to the worker.

This car object can be found at worker file by doing something like
anyJsFile.js
onmessage = function (e){
 var wCar = e.data;
 wCar.price = '$70000';
 wCar.company = 'Mercidies Benz';
 postMessage(wCar);
}
After modified the car object you can pass the modified object by postMessage(wCar) method  to main thread(main.js)

By the following code, you can get that modified data as e.data on main thread with onmessage method

anyWorker.onmessage = function (e){
 // get modified object from worker
 var mcar = e.data; 
 console.log(
 'Car model is ' + mcar.model + '\n' + 
 'Car color is ' + mcar.color + '\n' + 
 'Car price is ' + mcar.price + '\n' + 
 'Car company is ' + mcar.company + '\n' 
 );
}


The below points are worth to read

Same Origin Policy

The worker file and the code for an attempt to using that worker should be placed in same origin policy. Like anyWorker.js and main.js file should be located under same domain like http://javascriptfuture.com

DOM/BOM

The BOM(browser object model) and DOM(document object model) are not available for the web worker.

You can find the working example with Web-worker.


Saturday, March 7, 2015

What is nodejs?

Since long time I wanted to write about nodejs, So finally the day is came today.

Introduction
nodejs is a software by which we can use JavaScript on server side which means we can use the JavaScript for http request and can do read/write kind of operations on server.

It's not a web server like Apache, it does not do anything by itself, you have to create a server who handles about http request and response by using it's various modules.

nodejs is usually used for real time application, something like chat, audio, video etc. It is used for I/O kind of applications eg:- database reading/writing, make network connection etc. It uses the non-blocking concept for scalable network application. It uses V8 engine for great performance on server side. The V8 engine is made by google used in google-chrome browser for fast browser experience.

nodejs uses the JavaScript's asynchronous behavior(event loop), which means when you would apply the particular task for particular event and don't worry about that task, whenever that event is occurred that particular assigned task would be performed, so nodejs uses this nature of JavaScript for performance of scalable network application.
http requests for read operation

So here we would know how the nodejs increases the performance on scalable network  application.
As you can see in above image, there is multiple http requests for server santa, the request is about to read the content from database and response back to server and finally display the content in browser/computer.

With traditional way, the whole Process 1 http request for read page and response the page to server could take the long time, the Process 2 is not started until the Process 1 is finished. The RAM and it's CPU would be in idle state until the Process 1 is completed. So this blocking nature does waste the lot of RAM and CPU when large of users would come.

This kind of situation can be handled by multiple threads, However switching between threads and RAM does waste the memory and CPU.

JavaScript is single thread language, so how nodejs achieves huge performance let's see, as we already knew JavaScript has asynchronous behavior. At above figure, to start Process 2, there is no need to complete the Process 1. So second connection/http request can be made without completed first request which means the Process 2 could be started without finished Process 1.
We don't have wait here, So no extra expenses of RAM and CPU. That's how there could make a lot of concurrent connections at same time which increases the performance  on network scalable application .

Main Benefits

1) It uses the non-blocking concept.
2) It can handle lot of concurrent connections.
3) It uses the single thread for make lot of network connections.
4) Because of single thread, there is a lot of benefit on Memory and CPU of server.
5) Same context on client and server, which means we can use JavaScript both in client and server that we don't need to worry about the switching the programming language for client and server.

You can  get the nodejs from nodejs