Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are Javascript engines and runtimes and what are single-threaded and non-blocking, asynchronous and concurrent languages, respectively?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail what is the Javascript engine and runtime, as well as single-threaded and non-blocking and asynchronous and concurrent languages, respectively. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Yeah, what? I use language to finish my work. For me and Javascript, the process starts with building a mini game, ajax requests, and form validation. Our relationship with Hapi js is getting deeper and deeper. Ever since I met React, I have been working on. Now, to be a better programmer, you have to ask why things work. The Javascript runtime is one of the biggest mysteries for me.

What is the Javascript engine and runtime?

The Javascript runtime refers to where to execute when running JavaScript code. That is, you can execute javascript on google chrome, in which case your javascript engine is v8, if on mozilla-it's Spidermonkey, if it's IE-, then its chakra, if it's Safari-, it's nitro, if it's on a node, it's v8. What is the JS engine and what is the JS runtime now?

The engine converts the JavaScript we wrote into machine code. All JavaScript engines implement the language specification provided by ECMAScript. Standardization facilitates the development of stand-alone engines and ensures that your scripts get the same results wherever they are run. For speed, V8 converts JavaScript code into more efficient machine code instead of using an interpreter. It implements the JIT (just-in-time) compiler, as many modern JavaScript engines such as SpiderMonkey or Rhino (Mozilla) do, compiling JavaScript code into machine code at execution time. The main difference here is that V8 does not produce bytecode or any intermediate code. The JavaScript engine is just part of a larger concept. The engine works in an environment called Javascript Runtime, which provides additional functionality for our scripts. These features may include making VoIP calls, capturing mouse / keyboard events, and so on.

This is the architecture of JS Runtime. V8 does not have these WebAPI. These are given by the runtime. In the chrome browser JS runtime, the browser owns it, while in Node it is provided by the C + + library.

> Runtime Architecture

Let's look at how Javascript is asynchronous and single-threaded.

Single thread, what?

Javascript code executes in a single thread, but the Javascript runtime does not run in a single thread. The thread pool exists in the JS runtime, but we don't have to worry about it because the runtime handles it. But how is that done? The cycle of events can be saved.

Let's learn what the heap and call stack are in the runtime (or the JS engine that belongs to the runtime). The javascript code is first converted to machine code. The heap stores all variables and the operation is performed by the call stack.

Console.log ("Start") function sayHello (name) {console.log (`Hello ${name}! `)} sayHello ("Abhinav"); console.log ("End")

All of this goes to the call stack and executes there.

Start

Hello Abhinav

End

We can divide scripts into two types, immediate calls and later calls.

What happens when an asynchronous task arrives? The task takes time to run. Such as making API calls or timers, etc. There is a concept called a callback. This is the function to be performed after completing this task.

Well, they enter the call stack as any regular function, but since this task resides in WebAPI, we make a call to WebAPI. It stores the callback function of the task and completes the task for us (using threads / multiprocessing based on runtime). When the task is complete, it sends the callback to the callback queue.

Visualize again here.

What is the event loop now? The event loop runs continuously (it does not always run in the node during browser runtime) to check if the call stack is empty, and if the call stack is empty, it extracts the first item from the callback queue and moves it to the call stack and executes the callback function. No functionality is added from the callback queue until the stack is not empty.

Callbacks are always fully executed. The event loop runs one callback at a time. There is no context switch. All callbacks in the queue must wait until the current callback is complete. If the script takes too long to run, other scripts are blocked. This is why the callback should be relatively short and simple.

It's easy! But in fact, it's much more complicated. There are multiple queues, depending on the runtime, and they have different priorities. There are some things that serve as rendering queues. Whose job is to render the screen.

How is the non-blocking state?

Suppose you fail to call API, or some other event occurs, which still exists in Web api, so it never enters the callback queue and therefore does not enter the call stack. As a result, nothing is stopped.

Is it concurrent?

What's the difference between parallel and parallel? Parallelism is where you perform two tasks at the same time. Watch a movie while eating popcorn. This is achieved through multicore. JS code executes in parallel in the call stack, not in parallel. But WebAPI can take advantage of multiple cores and run in parallel.

On what is the Javascript engine and runtime and single-threaded and non-blocking and asynchronous and concurrent languages are shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report