|
1 | 1 |
|
2 | | -# Event loop: microtasks and macrotasks |
| 2 | +# Event loop: microtasks e macrotasks |
3 | 3 |
|
4 | | -Browser JavaScript execution flow, as well as in Node.js, is based on an *event loop*. |
| 4 | +Il flusso di esecuzione di Javascript, così come quello di Node.js, è basato su un *event loop*. |
5 | 5 |
|
6 | | -Understanding how event loop works is important for optimizations, and sometimes for the right architecture. |
| 6 | +Comprendere come un event loop lavora è importante per le ottimizzazioni, e a volte per creare delle architetture migliori. |
7 | 7 |
|
8 | | -In this chapter we first cover theoretical details about how things work, and then see practical applications of that knowledge. |
| 8 | +In questo capitolo, affronteremo i dettagli teorici su come funzionano, quindi, vedremo alcune applicazioni pratiche. |
9 | 9 |
|
10 | 10 | ## Event Loop |
11 | 11 |
|
12 | | -The concept of *event loop* is very simple. There's an endless loop, when JavaScript engine waits for tasks, executes them and then sleeps waiting for more tasks. |
| 12 | +Il concetto di *event loop* è molto semplice. Esiste un loop infinito nel quale il motore di Javascript rimane in attesa di un task (operazione) da eseguire, lo esegue, dopodichè si rimette in rimane in sleep (inattivo, dormiente, ma pronto per essere eseguito) per altri tasks. |
13 | 13 |
|
14 | | -The general algorithm of the engine: |
| 14 | +Questo, l'algoritmo generico del motore: |
15 | 15 |
|
16 | | -1. While there are tasks: |
17 | | - - execute them, starting with the oldest task. |
18 | | -2. Sleep until a task appears, then go to 1. |
| 16 | +1. Fino a quando ci sono task: |
| 17 | + - eseguili, cominciando da quello meno recente. |
| 18 | +2. Rimani in attesa fino a quando non c'è un altro task da eseguire, quindi vai al passo 1. |
19 | 19 |
|
20 | | -That's a formalization for what we see when browsing a page. JavaScript engine does nothing most of the time, only runs if a script/handler/event activates. |
| 20 | +Questa è una esposizione di quello che vediamo quando navighiamo una pagina. Il motore di Javascript non fa nulla per la maggiorparte del tempo, va in esecuzione quando si attiva uno script/handler/evento. |
21 | 21 |
|
22 | | -Examples of tasks: |
| 22 | +Esempio di tasks: |
23 | 23 |
|
24 | | -- When an external script `<script src="...">` loads, the task is to execute it. |
25 | | -- When a user moves their mouse, the task is to dispatch `mousemove` event and execute handlers. |
26 | | -- When the time is due for a scheduled `setTimeout`, the task is to run its callback. |
27 | | -- ...and so on. |
| 24 | +- Quando uno script esterno `<script src="...">` viene caricato (load), il task è quello di eseguirlo. |
| 25 | +- Quando un utente sposta il puntatore del mouse, il task è quello di fare il dispatch dell'evento `mousemove` ed eseguirne eventuali handlers (gestori). |
| 26 | +- Quando è scaduto il tempo per `setTimeout` già schedulato, il task è quello di eseguirne la callback. |
| 27 | +- ...e così via. |
28 | 28 |
|
29 | | -Tasks are set -- the engine handles them -- then waits for more tasks (while sleeping and consuming close to zero CPU). |
| 29 | +I task vengono impostati -- il motore li gestisce -- quindi rimane in attesa per altri tasks (nel frattempo rimane in sleep, consumando risorse della CPU prossime allo zero). |
30 | 30 |
|
31 | | -It may happen that a task comes while the engine is busy, then it's enqueued. |
| 31 | +Potrebbe succedere che mentre il motore è occupato arrivi un task, in questo caso, questo viene messo in coda. |
32 | 32 |
|
33 | | -The tasks form a queue, so-called "macrotask queue" (v8 term): |
| 33 | +I task formano una coda, la cosiddetta "macrotask queue" (termine di v8, il motore di Javascript di Chrome e di Node.js): |
34 | 34 |
|
35 | 35 |  |
36 | 36 |
|
37 | | -For instance, while the engine is busy executing a `script`, a user may move their mouse causing `mousemove`, and `setTimeout` may be due and so on, these tasks form a queue, as illustrated on the picture above. |
| 37 | +Ad esempio, se mentre il motore è occupato nell'esecuzione di uno `script`, l'utente muove il mouse generando un `mousemove`, e magari nello stesso istante è scaduto il tempo di un `setTimeout`, questi task formano una queue (una coda di esecuzione) come illustrato nella figura di sopra. |
38 | 38 |
|
39 | | -Tasks from the queue are processed on "first come – first served" basis. When the engine browser is done with the `script`, it handles `mousemove` event, then `setTimeout` handler, and so on. |
| 39 | +I task dalla coda vengono processati sulla base del "first come – first served", cioè secondo l'ordine che il primo arrivato sarà il primo ad essere servito (FIFO). |
| 40 | +Quando il motere del browser avrà terminato con lo `script`, gestirà l'evento `mousemove`, quindi si occuperà del gestore del `setTimeout` (la callback), e così |
40 | 41 |
|
41 | | -So far, quite simple, right? |
| 42 | +Fino a qui abbastanza semplice, giusto? |
42 | 43 |
|
43 | | -Two more details: |
44 | | -1. Rendering never happens while the engine executes a task. Doesn't matter if the task takes a long time. Changes to DOM are painted only after the task is complete. |
45 | | -2. If a task takes too long, the browser can't do other tasks, process user events, so after a time it raises an alert like "Page Unresponsive" suggesting to kill the task with the whole page. That happens when there are a lot of complex calculations or a programming error leading to infinite loop. |
| 44 | +Ancora due dettagli: |
| 45 | +1. Il rendering non avviene mai quando il motore sta eseguendo un task. Non importa se questo impiega molto tempo. I cambiamenti al DOM vengono renderizzati (ridisegnati sul browser) solo dopo che il task viene completato. |
| 46 | +2. Se un task impiega troppo tempo, il browsere non puiò eseguire altri taskm, processare eventi utente, e così dopo un certo periodo di tempo viene scaturito un alert di "Pagina bloccata"(Page Unresponsive) che ci suggerisce di terminare il task con l'intera pagina. Questo succede in concomitanza di una serie di calcoli complessi, o degli errori di programmazione che portano ad un loop infinito. |
46 | 47 |
|
47 | | -That was a theory. Now let's see how we can apply that knowledge. |
| 48 | +Questa era la teoria. Adesso vediamo come applicare questi concetti. |
48 | 49 |
|
49 | | -## Use-case 1: splitting CPU-hungry tasks |
| 50 | +## Caso d'uso 1: Spezzettamento di task affamati di CPU (processi intensivi) |
50 | 51 |
|
51 | | -Let's say we have a CPU-hungry task. |
| 52 | +Poniamo il caso che abbiamo un task affamato di CPU. |
52 | 53 |
|
53 | | -For example, syntax-highlighting (used to colorize code examples on this page) is quite CPU-heavy. To highlight the code, it performs the analysis, creates many colored elements, adds them to the document -- for a big text that takes a lot of time. |
| 54 | +Per esempio, la syntax-highlighting (usata per colorare ed evidenziare gli esempi del codice in questa pagina) è abbastanza pesante per la CPU. |
| 55 | +Per evidenziare il codice, compie delle analisi, crea molti elementi colorati, e li aggiunge al documento -- un testo di grosse dimensioni può impiegare molto tempo. |
54 | 56 |
|
55 | 57 | While the engine is busy with syntax highlighting, it can't do other DOM-related stuff, process user events, etc. It may even cause the browser to "hiccup" or even "hang" for a bit, which is unacceptable. |
56 | 58 |
|
|
0 commit comments