JavaScript VS C# threading model

We were developing an application, which emulates the functionality of a real time device. In this application, we need real time event; we pool the service by a certain time interval and need to send some key presses. Moreover, we have a large number of timers to show real time effects of device by manipulating DOM elements.

Our assumption was that each timer of JavaScript runs in separate thread and asynchronously as c# and any windows programming language do. However, we were wrong and all the timers run in the same thread in serialize manner.

We put a callback in asynchronous service call to retrieve the result when service call back. In c# and any windows programming language, each callback runs in a separate thread instead of main (UI) thread. In JavaScript, all callbacks run in the main (only) thread.

What problems have we faced? As JavaScript is a single threaded model and we have a large number of JavaScript timer to retrieve real time status and to manipulate UI, UI becomes very slow. It is because some of the timer function has loops and bad code that stop the main thread to proceed.

The following are the main points to remember:

  • JavaScript is a single threaded model where as C# is a multi-threaded model.
  • In JavaScript, all the timers run in the same thread but in C# each timer run in the different thread.
  • In JavaScript, all callbacks run in the main (only) thread but in c#, each callback runs in a separate thread instead of main (UI) thread. 

If you does not keep this in mind, there is a possibility that you will end up with UI, which is not refreshing fast enough.

No Comments