Is the javascript engine single-threaded?

WBOY
Release: 2022-03-03 10:35:37
Original
1612 people have browsed it

The JavaScript engine is single-threaded; the main purpose of JavaScript is to interact with users and operate the DOM. For example, to avoid complex synchronization problems, so that it can only do one thing at the same time, so JavaScript is single-threaded. .

Is the javascript engine single-threaded?

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

Is the JavaScript engine single-threaded?

The JavaScript engine is single-threaded.

Why is JS single-threaded? Why does it need asynchronous? How does single-thread realize asynchronous?

The emergence of technology is closely related to the application scenarios in the real world.

Similarly, we will answer the question based on real-life scenarios

(1) Why is JS single-threaded?

Single-threaded: Same You can only do one thing at a time

The single thread of JavaScript is related to its purpose. As a browser scripting language, JavaScript's main purpose is to interact with users and manipulate the DOM. This determines that it can only be single-threaded, otherwise it will cause very complex synchronization problems. For example, suppose JavaScript has two threads at the same time. One thread adds content to a certain DOM node, and the other thread deletes the node. In this case, which thread should the browser use?

In order to take advantage of the computing power of multi-core CPUs, HTML5 proposes the Web Worker standard, which allows JavaScript scripts to create multiple threads, but the child threads are completely controlled by the main thread and must not operate the DOM. Therefore, this new standard does not change the single-threaded nature of JavaScript.

JS was originally designed to be used in browsers, so imagine if JS in the browser was multi-threaded.

Scenario description:

So now there are 2 processes, process1 and process2. Since it is a multi-process JS, they operate on the same dom at the same time

process1 Delete The DOM is edited, and process2 edits the DOM and issues two contradictory commands at the same time. How should the browser execute it?

Thinking about it this way, it should be easy to understand why JS is designed to be single-threaded.

(2) Why does JS need asynchronous?

Scene description:

If asynchronous does not exist in JS, it can only be executed from top to bottom. If the previous line takes a long time to parse, the code below will be blocked.

For users, blocking means "stuck", which leads to a poor user experience

So, there is asynchronous execution in JS.

Related recommendations: javascript learning tutorial

The above is the detailed content of Is the javascript engine single-threaded?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template