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

Is javascript engine single threaded?

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

Share

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

This article is about whether javascript engines are single-threaded. Xiaobian thinks it is quite practical, so share it with everyone for reference. Let's follow Xiaobian and have a look.

JavaScript engines are single-threaded;JavaScript's main purpose is to interact with users and manipulate the DOM, for example, to avoid complex synchronization problems and make it do only one thing at a time, so JavaScript is single-threaded.

Operating environment of this tutorial: Windows 10 system, Javascript version 1.8.5, Dell G3 computer.

Is JavaScript engine single-threaded?

The JavaScript engine is single-threaded.

Why is JS single-threaded? Why asynchronous? How does a single thread work asynchronously?

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

Again, we're going to answer the question in context of reality.

(1)Why is JS single-threaded?

Single thread: only one thing can be done at a time

JavaScript is single-threaded, depending on its purpose. As a browser scripting language, JavaScript is primarily used to interact with users and manipulate the DOM. This determines that it can only be single-threaded, otherwise it will bring very complex synchronization problems. For example, suppose JavaScript has two threads at the same time, one adding content to a DOM node and the other deleting it, which thread should the browser take over?

To take advantage of the computing power of multicore CPUs, HTML5 introduced the Web Worker standard, which allows JavaScript scripts to create multiple threads, but child threads are completely controlled by the main thread and must not manipulate the DOM. So, this new standard doesn't change the single-threaded nature of JavaScript.

JS was originally designed for use in browsers, so imagine if JS in browsers were multithreaded.

Scene Description:

So now there are two processes,process1 process2, and since it's multi-process JS, they're operating on the same dom at the same time.

Process1 deletes the dom, process2 edits the dom, and issues two contradictory commands. How does the browser execute them?

It should be easy to understand why JS is designed to be single-threaded.

(2)Why does JS need asynchronous?

Scene Description:

If there is no asynchronous in JS, it can only be executed from top to bottom, and if the previous line takes a long time to parse, then the following code will be blocked.

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

So, asynchronous execution exists in JS.

Thank you for reading! About "javascript engine is not a single thread" this article is shared here, I hope the above content can have some help for everyone, so that we can learn more knowledge, if you think the article is good, you can share it to let more people see it!

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