고정된 트윗

"99% of Javascript developers think async is parallel"
-> Javascript is single threaded which means that any operation will block the main thread
-> Even "console.log" blocks the main thread if spammed
-> Worker threads is the only way to get parallelism in javascript
How does Javascript work?
-> Javascript internally uses a Call Stack (LIFO), last on the stack will be executed first.
-> If the function takes too long it will block the thread
So why does the UI not freeze?
-> While javascript is single threaded, the environment it runs on (Chrome, Firefox, Node.js) are multi threaded.
-> For the most part JS hands over the task to the Browsers APIs, allowing async tasks to run.
-> Once task is finished it is moved to a queue (eg. macro task queue)
-> And this process keeps going. Through this JS can stay single threaded while the browser does all the task.
And why is this not considered parallel?
-> Because this is not parallel execution
-> But task assignment and waiting for it to finish is a totally different concept known as Event Loop which is not parallel
So where does the issue come?
-> When we're not using the Browser APIs.
-> As then the JS needs to run code on it's own in a single threaded environment.
-> If the task is big the thread is blocked!
-> Examples: "JSON.parse", or maps on huge arrays. are simple silent killers

English










