JavaScript is a single-threaded language. In interpreting the event loop before first to explain the browser’s thread of execution: the browser is multiple processes, each browser TAB label represents a separate process, including the browser rendering process (browser kernel) belong to the browser > one of multiple processes, mainly be responsible for the page rendering, script execution, event handling, etc It contains threads are: GUI rendering threads (rendering pages, parsing HTML, CSS forming DOM trees), JS engine threads, event-triggering threads, > timer triggering threads, HTTP request threads and other main threads

About executing threads:

Main thread: this is the js engine execution thread, this thread is only one, page rendering, function processing is performed on this main thread. Worker thread: Also called background thread, this thread may exist in the browser or JS engine, separate from the main thread, processing asynchronous events such as file reads, network requests, etc.

Microtask diagram

 console.log('script start');

 setTimeout(function() {
   console.log('timeout1');
 }, 10);

 new Promise(resolve= > {
     console.log('promise1');
     resolve();
     setTimeout(() = > console.log('timeout2'), 10);
 }).then(function() {
     console.log('then1')})console.log('script end');

 // The main process is a synchronization task
 // Microtasks include Promise, MutaionObserver, process.nexttick
 // An asynchronous access to the event queue macro task mainly includes: Script, setTimeout, setInterval, I/O, UI interaction events, setImmediate(node.js environment)

 Macro task 1: script start => promise1 => script end Enable macro task 2
 Then1 starts macro task 3. Then1 starts macro task 3
 // 3. Macro task 2: timeout1
 // 4. Macro task 3: timeout2

 // TMASTER(key): Each time the macro task in the task queue is read, the micro task is emptied first.
Copy the code

  • Here’s a tip: MicroTask execution takes precedence over Task execution by specification, so if there is logic that needs to be executed first, putting it into the MicroTask queue will be executed before the task.

  • Finally, JS is a single-threaded language. Asynchronous operations are placed in the event loop queue, waiting for the main execution stack to execute, and there is no special asynchronous execution thread.


// Bubble sort
function sort (arr) {
    for (var i = 0; i < arr.length - 1; i++) {
        for (var j = 0; j < arr.length - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                var temp = arr[j]
                arr[j] = arr[j + 1];
                arr[j + 1] = temp; }}}return arr;
}

console.log(sort([5.3.4.7.2.9.1]))

// Select sort

function chooseSort(arr) {
    
    for (var i = 0; i < arr.length - 1; i++) {
        var min = i; // minimum subscript
        for (var j = i + 1; j < arr.length - 1; j ++) {
            if(arr[j] < min) { min = j; }}var temp = arr[i];
        arr[i] = arr[min];
        arr[min] = temp;
        
    }
    return newArr
} 
Copy the code