Skip to content

Commit 016a48d

Browse files
committed
an overview of the engine, the runtime, and the call stack
1 parent a23247d commit 016a48d

File tree

1 file changed

+57
-1
lines changed

1 file changed

+57
-1
lines changed

Callstack/README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
# The JavaScript engine
2+
3+
A popular example of a JavaScript engine is Google's V8. The V8 is used inside Google Chrome, and Node.js for example. Here is the presentation of what it looks like:
4+
5+
![JS Engine](https://miro.medium.com/max/1400/1*OnH_DlbNAPvB9KLxUCyMsA.png)
6+
7+
As you can see, the engine consits two main components:
8+
9+
- Memory Heap: where the memory allocation happens
10+
- Call stack: where your stack frames are as your code executes.
11+
12+
# The Runtime
13+
14+
There are a myriad of APIs provided by the browser that have been used by almost any JavaScript developer out there (e.g: setTimeout()). Yeah, those APIs are not provided by engine!
15+
16+
![Other stuff](https://miro.medium.com/max/1400/1*4lHHyfEhVB0LnQ3HlhSs8g.png)
17+
18+
Beyond the engine, we also have other stuffs call Web APIs which are provided by browsers, like DOM, AJAX, setTimeout and much more.
19+
20+
And, we have `event loop`, and the `callback queue`.
21+
122
# Callstack
223

324
Callstack is a data structure recording the function calls. Particularly, if we call a function to execute, we push it on to the stack, and when we return from a function, we pop off it the top of the stack.
@@ -10,4 +31,39 @@ We get, sometimes, into an infinite loop as we call a function multiple times re
1031

1132
There is, in Chrome browser, a limit on the size of the stack which is 16000 frames, more than that it will throw <b>Max Stack Error Reached</b>
1233

13-
P/s: Check the folder five things for more details.
34+
JavaScript is a single-threaded programming language, that means it has a single Call Stack. Therefore it can do one thing at a time.
35+
36+
Let's see an example
37+
38+
```javascript
39+
function multiply(x, y) {
40+
return x * y;
41+
}
42+
43+
function printSquare(x) {
44+
var s = multiply(x, x);
45+
console.log(s);
46+
}
47+
48+
printSquare(5);
49+
```
50+
51+
As we've described above, here is what actually happens in the call stack.
52+
53+
![stack frames](https://miro.medium.com/max/1400/1*Yp1KOt_UJ47HChmS9y7KXw.png)
54+
55+
Running code on a single thread can be quite easy since you don’t have to deal with complicated scenarios that are arising in multi-threaded environments — for example, deadlocks.
56+
57+
But running on a single thread is quite limiting as well. Since JavaScript has a single Call Stack, what happens when things are slow?
58+
59+
# Concurrency & the Event Loop
60+
61+
What happens when you have function calls in the Call Stack that take a huge amount of time in order to be processed? For example, imagine that you want to do some complex image transformation with JavaScript in the browser.
62+
63+
You may ask — why is this even a problem? The problem is that while the Call Stack has functions to execute, the browser can’t actually do anything else — it’s getting blocked. This means that the browser can’t render, it can’t run any other code, it’s just stuck. And this creates problems if you want nice fluid UIs in your app.
64+
65+
![Unresponsive](https://miro.medium.com/max/924/1*WlMXK3rs_scqKTRV41au7g.jpeg)
66+
67+
And that’s not the only problem. Once your browser starts processing so many tasks in the Call Stack, it may stop being responsive for quite a long time. And most browsers take action by raising an error, asking you whether you want to terminate the web page.
68+
69+
So, how can we execute heavy code without blocking the UI and making the browser unresponsive? Well, the solution is asynchronous callbacks.

0 commit comments

Comments
 (0)