Skip to content

Commit 2f7d710

Browse files
committed
add jre notes
1 parent 5cd685f commit 2f7d710

File tree

10 files changed

+151
-26
lines changed

10 files changed

+151
-26
lines changed

session 1/chapters/0. Execution Context.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,35 @@ JavaScript is basically ECMAScript at its core but builds upon it. Languages suc
1515

1616
**ECMAScript is a standard and that JavaScript is a well-known implementation of that standard**
1717

18+
# Javascript Engine
19+
20+
JavaScript engine will parse the code line by line, convert it into machine code and then execute it. Some of the well-known JS engines are listed below:
21+
22+
![jsEngine](../images/jsengine.jpg)
23+
24+
![engineComponents](../images/engineComponents.jpg)
25+
26+
## Execution context stack (ECS)
27+
28+
1. Execution context stack is a stack data structure which follows the Last In First Out (LIFO) principle
29+
2. ECS stores the execution context for each function
30+
3. Execution context is defined as an object which stores local variables, functions and objects
31+
4. Primitive values like int, bool etc are stored inside the execution context object while function definitions and objects are not stored inside the execution context object, they are stored inside the heap. Execution context object just has the reference or memory address of where these function definitions and objects are stored.
32+
33+
**JavaScript engine always executes the function which is at the top of the execution context stack.**
34+
35+
By default, at the bottom of the ECS, we have a global execution context which deals with all the code in the global scope. Each function has its own execution context called functional execution context which gets inserted on the top of ECS as and when the function is called in the code. If the same function is called twice like in recursion, it will have two different functional execution context in the ECS.
36+
37+
When the execution of the function is completed, JS engine itself removes it from the ECS and starts executing the function on the top of the stack.
38+
39+
**As JavaScript engine has only one ECS, it can execute only one thing at a time which is at the top of the ECS. This is what makes JavaScript single threaded.**
40+
41+
## Heap
42+
43+
Heap is a large unstructured data structure which stores all the dynamic data like function definitions, objects, arrays etc.
44+
45+
The memory occupied in the heap continues to exist even after the JavaScript code execution has completed. They are removed by the JavaScript Garbage Collector.
46+
1847
# Execution Context
1948

2049
## Javascript is synchronous single threaded language
@@ -37,8 +66,6 @@ So its made of two parts
3766
1. Memory - Store variables/functions etc.
3867
2. Thread of execution - the code currently being executed
3968

40-
![execution context](../images/0.executionContextComponenets.jpg)
41-
4269
![execution context](../images/1.executionContext.jpg)
4370

4471
## Types of execution context

session 1/chapters/1. JRE.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# JavaScript Runtime Environment (JRE)
2+
3+
JavaScript engine doesn’t run in isolation. It runs inside an environment called JavaScript Runtime Environment along with many other components. JRE is responsible for making JavaScript asynchronous. It is the reason JavaScript is able to add event listeners and make HTTP requests asynchronously.
4+
5+
JRE is just like a container which consists of the following components:
6+
7+
1. JS Engine
8+
2. Web API
9+
3. Callback Queue or message queue
10+
4. Event Table
11+
5. Event loop
12+
13+
![Jre](../images/jre.jpeg)
14+
15+
## Web API
16+
17+
Web APIs are not part of the JS engine but they are part of the JavaScript Runtime Environment which is provided by the browser. JavaScript just provides us with a mechanism to access these API’s.
18+
19+
When JavaScript engine finds any Web API method, it sends that method to an event table, where it waits till an event occurs.
20+
21+
In the case of AJAX calls, the JS engine will send the AJAX calls to the event table and will continue the execution of code after the Ajax call. AJAX call will wait in the event table until the response from the AJAX call is returned.
22+
23+
In case of timer function like setTimeout, it waits until the timer count becomes zero.
24+
25+
## Event Table
26+
27+
It is a table data structure to store the asynchronous methods which need to be executed when some event occurs(timeout, click, mouse move).
28+
29+
Event Table does not execute functions and does not add them to the call stack on it’s own. It’s sole purpose is to keep track of events and send them to the Event Queue.
30+
31+
## Callback Queue or Message Queue or Event Queue
32+
33+
These queues follows First In First Out principle (item to be inserted first in the queue will be removed from the queue first). It stores the correct order in which the functions should be executed.
34+
35+
It stores all the messages which are moved from the event table to the event queue. Each message has an associated function.
36+
37+
It receives the function calls from the Event Table, but it needs to somehow send them to the Call Stack? This is where the Event Loop comes in.
38+
39+
## Event Loop
40+
41+
Methods are executed neither in the event table nor in the event queue. They are executed by the JavaScript engine, only if it is present in the ECS.
42+
43+
So, for the execution of any method, engine needs to move that method from the callback queue to the execution context stack. This is what the event loop does!
44+
45+
Event loop continuously checks if the execution context stack is empty and if there are any messages in the event queue.
46+
47+
**It will move the method from the callback queue to ECS only when the execution context stack is empty.**
48+
49+
```javascript
50+
function printStatement() {
51+
console.log('I should be printed after timeout');
52+
}
53+
54+
setTimeout(printStatement, 0);
55+
56+
console.log('I will be executed first');
57+
```
58+
59+
Some people think that because set timeout is called with 0 (zero) it should run immediately. In fact in this specific example you will see “second” printed out before “first”. JavaScript sees the setTimeout and says “Well, I should add this to my Event Table and continue executing”. It will then go through the Event Table, Event Queue and wait for the Event Loop to tick in order to run.
60+
61+
```javascript
62+
var log = console.log;
63+
64+
log('Inside global execution context');
65+
66+
function functionOne() {
67+
log('Inside function one');
68+
69+
function setTimeoutFunction() {
70+
log('Inside setTimeoutFunction: I will be executed atleast after 1 sec');
71+
}
72+
73+
setTimeout(setTimeoutFunction, 1000);
74+
75+
for (var i = 0; i < 100000000; i++) {
76+
// Blocking code. This makes the for loop to execute for more than 1 second
77+
// Still setTimeoutFunction is not executed. It gets executed only after
78+
// last statement of the code
79+
}
80+
81+
log('Exiting functionOne');
82+
}
83+
84+
functionOne();
85+
86+
log('Exiting global execution context');
87+
```
88+
89+
![setTimeout](../images/setTimeOut.gif)
90+
91+
If the execution context stack is not empty, the event loop will not move the message from the callback queue to the execution context stack.
92+
93+
**Thus, the time passed to `setTimeout` function does not guarantee its execution after the elapse of that time, but it is the minimum time after which setTimeout callback function will be executed.**
94+
95+
**From the above example, we can understand the following things**
96+
97+
1. JavaScript is single thread i.e. at any instant of time it can execute only a single piece of code.
98+
2. JS engine executes the function which is at the top of the stack
99+
3. Asynchronous jobs like waiting for the setTimeout callback function to execute is not done by the JavaScript engine itself. It is done by the JavaScript run time environment
File renamed without changes.

session 1/chapters/references.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
- https://dmitripavlutin.com/6-ways-to-declare-javascript-functions/
88

99
- https://medium.com/@happymishra66/execution-context-in-javascript-319dd72e8e2c#:~:text=Execution%20context%20(EC)%20is%20defined,to%20at%20a%20particular%20time.
10+
11+
- https://blog.bitsrc.io/javascript-internals-javascript-engine-run-time-environment-settimeout-web-api-eeed263b1617
53.6 KB
Loading

session 1/images/jre.jpeg

65.6 KB
Loading

session 1/images/jsengine.jpg

23.2 KB
Loading

session 1/images/setTimeOut.gif

394 KB
Loading

session 1/server/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<head>
2-
<script src="/index.js"></script>
2+
<script src="index.js"></script>
33
</head>
44
<body>
55
<h1>Hello World</h1>

session 1/server/index.js

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
1-
const calculator = (a, b) => {
2-
function add() {
3-
return a + b;
4-
}
5-
function subtract() {
6-
return a - b;
7-
}
8-
function multiply() {
9-
return a * b;
1+
var log = console.log;
2+
3+
log('Inside global execution context');
4+
5+
function functionOne() {
6+
log('Inside function one');
7+
8+
function setTimeoutFunction() {
9+
log('Inside setTimeoutFunction: I will be executed atleast after 1 sec');
1010
}
11-
function divide() {
12-
return a / b;
11+
12+
setTimeout(setTimeoutFunction, 1000);
13+
14+
for (var i = 0; i < 100000000; i++) {
15+
// Blocking code. This makes the for loop to execute for more than 1 second
16+
// Still setTimeoutFunction is not executed. It gets executed only after
17+
// last statement of the code
1318
}
1419

15-
return {
16-
add: add,
17-
subtract: subtract,
18-
multiply: multiply,
19-
divide: divide,
20-
};
21-
};
20+
log('Exiting functionOne');
21+
}
2222

23-
const cal = calculator(12, 6);
23+
functionOne();
2424

25-
console.log(cal.add());
26-
console.log(cal.subtract());
27-
console.log(cal.multiply());
28-
console.log(cal.divide());
25+
log('Exiting global execution context');

0 commit comments

Comments
 (0)