You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Callstack/README.md
+57-1Lines changed: 57 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff 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:
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!
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
+
1
22
# Callstack
2
23
3
24
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
10
31
11
32
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>
12
33
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
+
functionmultiply(x, y) {
40
+
return x * y;
41
+
}
42
+
43
+
functionprintSquare(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.
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.
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