JavaScript: The Runtime EnvironmentπŸƒπŸ»πŸƒπŸ»πŸƒπŸ»

rahmanmajeed

Majedur Rahman

Posted on October 7, 2023

JavaScript: The Runtime EnvironmentπŸƒπŸ»πŸƒπŸ»πŸƒπŸ»

So you may know that in JavaScript, your code somehow compiles and executes in your browser or server to display the beautiful application you've built. But are you aware of all the components that come into play to produce the output?

let's dive into deep and see what happens behind the hood in JavaScript and how it's run.

first, we're just glance over the runtime definition.

What's a Runtime?

A runtime is the environment in which a programming language executes. The runtime system facilitates storing functions, variables, and managing memory by using data structures such as queues, heaps and stacks.

JavaScript Runtime Environment

JavaScript runtime environment is like a container, that has all the things to run a JavaScript Code.There are two types of JavaScript runtime environment:

  1. The runtime environment of a Browser (like Google Chrome).
  2. The Node runtime environment.

Every Browser has it's own JS Runtime Environment. The heart of any JavaScript Runtime is always JavaScript Engine. Without an engine there is no runtime and no JavaScript at all.

A JavaScript Runtime consists of the following components-

  • [πŸš€] The JS Engine.
  • [🌏] Web/Global APIs.
  • [βŒ›] Callback Queue.
  • [πŸ”¬] Microtask Queue.
  • [πŸ”„] Event Loop.

Here, we can glimpsed the JavaScript Runtime Environment as a whole in one picture.

Image description

1. The JS Engine πŸš€
The JavaScript engine, is a vital component of the JavaScript Runtime Environment. Consider it heart of the JavaScript runtime environment. Operating in a single-threaded manner,it executes tasks one at a time, following a specific order. There are a lot of steps involved in doing that, but essentially executing JavaScript code is what an engine does.
Every browser has its own JavaScript engine but the most well known engine is Google's V8. The V8 engine powers Google Chrome but also Node.js which is that JavaScript Runtime.

2. Web/Global APIs 🌏
Web/Global APIs play a crucial role in the JavaScript Runtime Environment by enabiling the execution of tasks in the background, creating the illusion of a multi-threaded environment. It's not part of the JS Engine rather it's crucial part of Runtime environment. These APIs, provided by the browser or the operating system, allow JavaScript to interact with external resources and perform operations asynchronously.

Queue
The queue is an essential part of the JavaScript task scheduling algorithm, which dertermines the order in which tasks are executed by the JavaScript Engine.Both queues hold tasks that are executed asynchronously, but there are some differences in how they work.

3. Callback Queue (Macrotask Queue) βŒ›
The Callback Queue also known as the task queue, holds tasks that are pushed to the queue by Web/Global APIs, such as setTimeout,setInterval,XMLHttpRequest, or Events like mouse clicks and keyboard inputs. When the call stack is empty & microtask queue has no priority task, the event loop moves the first task from callback queue to the call stack for execution.
For example:



console.log('script step-1');
setTimeout(() => console.log('script step-2'), 0);
console.log('script step-3');


Enter fullscreen mode Exit fullscreen mode

4. Microtask Queue πŸ”¬
The Microtask Queue, also known as the Job Queue or Promise Queue. It's handle the mutation observer callbacks and promise callbacks. These tasks are scheduled to run after the currently executing script is complete and before the event loop continues with the next task in the task queue.The microtask queue is processed before the callback queue, so the tasks in the microtask queue are given priority over tasks in the callback queue.



console.log('script start');

setTimeout(() => {
  console.log('setTimeout');
}, 0);

Promise.resolve().then(() => {
  console.log('promise1');
}).then(() => {
  console.log('promise2');
});

console.log('script end');


Enter fullscreen mode Exit fullscreen mode

5. Event Loop πŸ”„
The event loop is a continuous process that constantly checks the call stack and the callback queue,microtask queue. Event loop takes callback functions from the callback queue,microtask queue and puts them in the call stack. So that they can be executed. Also Event Loop is essential for non-blocking concurrency model.

Event Loop

we'll explore the every components from JavaScript Runtime Environment in upcoming posts.
For now, let's end it here.

πŸ’– πŸ’ͺ πŸ™… 🚩
rahmanmajeed
Majedur Rahman

Posted on October 7, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related