Home  >  Article  >  Web Front-end  >  JavaScript running mechanism: Why JavaScript is single-threaded

JavaScript running mechanism: Why JavaScript is single-threaded

php中世界最好的语言
php中世界最好的语言Original
2018-03-16 15:25:282352browse

This time I will bring you JavaScriptWhy JavaScript is single-threaded in the running mechanism, When using JavaScriptrunning mechanismWhat are the precautions, the following is the actual combat Let’s take a look at the case.

JavaScriptA major feature of the language is single-threading, which means that only one thing can be done at the same time. So why can't JavaScript have multiple threads? This can improve efficiency.

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, assuming that JavaScript has two threads at the same time, one thread adds content to a certain DOM node, and the other threaddeletethe node, then which thread should the browser use as the allow?

So, in order to avoid complexity, JavaScript has been single-threaded since its birth. This has become the core feature of this language and will not change in the future.

In order to take advantage of the computing power of multi-core CPUs, HTML5 proposed 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.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to the php Chinese website Other related articles!

Recommended reading:

Implicit type conversion in JS

Usage of Nodejs routing and controller

Nodejs uses Mongoose to create models and APIs

The above is the detailed content of JavaScript running mechanism: Why JavaScript is single-threaded. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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