How JavaScript as a Back-end Language.

Let's look at JavaScript runtime.

Let's see what the heck is JavaScript.

JavaScript (JS) is a lightweight, interpreted, or just-in-time compiled programming language. It has come into the picture to make the web pages in your browser interactive without the computation cost for the client-server transaction.

Notice, it's a JIT-compiled programming language, which means it compiles the code which needs to be run at any particular moment in time.

"JavaScript is a single-threaded language"

Now, what is single-threaded mean? But, before that what is a thread mean?

A "thread" in programming language means the execution of running multiple tasks or programs at the same time. More simply a path for the compiler to compile all the code into machine language.

Now, Single-threaded is a kind of self-explanatory. It's like a one-way road until the last line of code. At this point, we might have a question. Why is that a problem?

Why is single-thread a problem?

Let's understand why. Suppose we are on a website which helps us to order PIZZA, and the website got good traffic, which means many users. What if five users came and started to order multiple pizzas all at the same time? So, our code will handle one user at a time, as JAVASCRIPT IS SINGLE-THREADED. The four other users need to wait in the queue, don't you think it would be more efficient to take the orders for all of them together and start making the pizzas separately?

That's where our system got overloaded and doesn't seem to work as we wished it to.

Now if you have understood this much, you must have a question. How we are using JavaScript as a back-end language to handle APIs which are continuously fetching and serving data?

Here comes NODE JS into the picture

Node.js is a JavaScript runtime environment that achieves low latency and high throughput by taking a “non-blocking” approach to serving requests. In other words, Node.js wastes no time or resources waiting for I/O requests to return.

  • What is a runtime environment?

    A runtime environment is where our program is executed, and what global variables are available to use, it can be a determining factor for the purpose you are writing the code. why?

    There are two types of JAVASCRIPT runtime :

    • Browser runtime environment - Javascript can run on your browser, and the script tags on your HTML files are the JS running on your browser. Remember window.alert("hello world") where we are getting this window from? this window is an object in our browser environment which has a function/method alert by which we are controlling what is on our browser screen. We call it Frontend Development.

    • Node runtime environment - The node is JS runtime environment which is developed for backend development such as having access to the file system, databases, and networks attached to the server, this runtime has different methods to carry out these processes.

Let's see what non-blocking is and what is I/O doing here.

Now, writing from a resource or reading from a resource is considered I/O operations. Any resource that is not accessible instantly at the machine instruction level but involves waiting instead requires I/O communication.

The reason we can use JS as a backend language is because of this non-blocking feature. Let's de-structured what is it.

To understand why non-blocking is working, let's see what is blocking doing. The blocking term originates from the operating system process model. A multitasking operating system labels each process with a state depending on how ready they are to be put on the CPU for execution. A process is labelled blocked if it is not ready for execution but is instead waiting for an I/O event to take place. I/O events indicate progress or completion in an I/O operation, for example, "resource available" or "write complete".

Using the blocking, the call will not return until the I/O is completed, which shares similarities with a single-threaded language. Remember our PIZZA website customers are waiting right?

Therefore Node implements the NON-BLOCKING approach. A non-blocking operation does not wait for I/O to complete. A non-blocking call initiates the operation and leaves it for the operating system to complete returning immediately without any results. Alternate means are then used to determine the completion of the I/O operation.

A performant application uses non-blocking I/O operations using javascript callback. This allows a single process to serve multiple requests at the same time. Instead of the process being blocked and waiting for a single I/O operation to complete, this waiting time is used to serve other requests. All Node.js libraries and the Node core API offer non-blocking operations. So our PIZZA website will take orders once it started to prepare the current order, we made our system concurrent and efficient at the same time.

So, this is how we are using JavaScript as a Back-end Language.

Reference for this article :