{"componentChunkName":"component---src-templates-blog-post-js","path":"/JavaScript/javascript-concept-8---event-loop/","webpackCompilationHash":"f92dbf25810602c1889e","result":{"data":{"site":{"siteMetadata":{"title":"Web Devlog","author":"RossenaHuh","siteUrl":"https://github.com/rossenahuh","comment":{"disqusShortName":"RossenaHuh","utterances":"rossenahuh/my-dev-blog.git"},"sponsor":{"buyMeACoffeeId":"rossena"}}},"markdownRemark":{"id":"2c07cb34-bfbc-586b-a50a-30c357c5abae","excerpt":"JavaScript is a single threaded programming languagesingle threadsingle call stackIts runtime can only process one piece of code at a timeAny code that is slow blocks the stack.For example console.log isn’t slow, but doing a while loop from one to ten billion is slow, doing image…","html":"<h2 id=\"javascript-is-a-single-threaded-programming-language\"><a href=\"#javascript-is-a-single-threaded-programming-language\" aria-label=\"javascript is a single threaded programming language permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>JavaScript is a single threaded programming language</h2>\n<ul>\n<li>single thread</li>\n<li>single call stack</li>\n<li>Its runtime can only process one piece of code at a time</li>\n</ul>\n<h2 id=\"any-code-that-is-slow-blocks-the-stack\"><a href=\"#any-code-that-is-slow-blocks-the-stack\" aria-label=\"any code that is slow blocks the stack permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Any code that is slow blocks the stack.</h2>\n<p>For example console.log isn’t slow, but doing a while loop from one to ten billion is slow, doing image processing is slow, and network request is slow.</p>\n<p>what happens when things are slow?</p>\n<p>The browser gets stucked. While the request is handled, the browser can’t render thereby users can’t do anything until those requests complete.</p>\n<h2 id=\"the-solution-asynchronous-callbacks\"><a href=\"#the-solution-asynchronous-callbacks\" aria-label=\"the solution asynchronous callbacks permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The solution? Asynchronous callbacks</h2>\n<div class=\"gatsby-highlight has-highlighted-lines\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'hello'</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\">// this part goes to WebAPIs</span>\n<span class=\"gatsby-highlight-code-line\"><span class=\"token function\">setTimeout</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span></span><span class=\"gatsby-highlight-code-line\">  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'there'</span><span class=\"token punctuation\">)</span></span><span class=\"gatsby-highlight-code-line\"><span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token number\">3000</span><span class=\"token punctuation\">)</span></span>\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'how are you?'</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\">// hello</span>\n<span class=\"token comment\">// how are you?</span>\n<span class=\"token comment\">// there</span></code></pre></div>\n<p>But how is this possible if JavaScript runtime is single threaded?\nBecause browser is more than just the Runtime. It provides us with <strong>WebAPIs</strong>, which we can make calls to. So it’s working as <strong>multi threads in the back</strong>. This diagram is basically identical for node, except the fact that it’s using C++ APIs instead of WebAPIs.</p>\n<p><img src=\"http://maxisam.github.io/2016/09/27/JavaScript-Note-Thread-Event-Loop/javascript_event_loop.png\" alt=\"JSEventloop\"></p>\n<h2 id=\"event-loops-job\"><a href=\"#event-loops-job\" aria-label=\"event loops job permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Event loop’s job</h2>\n<p>Event loop looks at the stack and the task queue. If the stack is empty, it takes the first thing on the queue and pushes it on to the stack.</p>\n<h2 id=\"the-order-of-proccessing-asynchronous-callback\"><a href=\"#the-order-of-proccessing-asynchronous-callback\" aria-label=\"the order of proccessing asynchronous callback permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The order of proccessing asynchronous callback</h2>\n<ol>\n<li>Pass the callback to webAPIs and pop off the callback from stack</li>\n<li>WebAPIs pushes the callback on to the task queue when it’s done</li>\n<li>Event loop checks if the call stack is empty</li>\n<li>If the call stack is cleared, event loop pushes the callback from the task queue to the call stack</li>\n</ol>\n<h2 id=\"settimeout-is-a-minimum-time-to-execution\"><a href=\"#settimeout-is-a-minimum-time-to-execution\" aria-label=\"settimeout is a minimum time to execution permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>setTimeout is a minimum time to execution</h2>\n<p>setTimeout is not guaranteed time to execution, it’s a <strong>minimum time to execution</strong>.</p>\n<div class=\"gatsby-highlight\" data-language=\"js\"><pre class=\"language-js\"><code class=\"language-js\">console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'hello1'</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\">// setTimeout zero doesn't run immediately</span>\n<span class=\"token function\">setTimeout</span><span class=\"token punctuation\">(</span><span class=\"token keyword\">function</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'hello2'</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span> <span class=\"token number\">0</span><span class=\"token punctuation\">)</span>\n\nconsole<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span><span class=\"token string\">'hello3'</span><span class=\"token punctuation\">)</span>\n\n<span class=\"token comment\">// hello1</span>\n<span class=\"token comment\">// hello3</span>\n<span class=\"token comment\">// hello2</span></code></pre></div>\n<br>\n<blockquote>\n<p>Conclusion : Don’t put slow code on the stack because when you do that, the browser can’t do what it needs to do, creating a nice fluid UI.</p>\n</blockquote>\n<br>\n<br>\n<p><em>Reference</em></p>\n<p><a href=\"https://www.youtube.com/watch?v=8aGhZQkoFbQ&#x26;t=821s\">https://www.youtube.com/watch?v=8aGhZQkoFbQ&#x26;t=821s</a></p>\n<br>\n<br>","frontmatter":{"title":"JavaScript Concept 8 - Event Loop","date":"July 08, 2019"}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"slug":"/JavaScript/javascript-concept-8---event-loop/","previous":{"fields":{"slug":"/JavaScript/javascript-concept-7---==-and-===/"},"frontmatter":{"title":"JavaScript Concept 7 - == and ===","category":"JavaScript"}},"next":{"fields":{"slug":"/JavaScript/javascript-concept-9---functional-programming-&-object-oriented-programming/"},"frontmatter":{"title":"JavaScript Concept 9 - Functional Programming & Object Oriented Programming","category":"JavaScript"}}}}}