-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsEngine&EventLoop.js
85 lines (60 loc) · 2.44 KB
/
JsEngine&EventLoop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Refer these video
/*
https://www.youtube.com/watch?v=8zKuNo4ay8E&list=PLlasXeu85E9cQ32gLCvAvr9vNaUccPVNP&index=18
https://www.youtube.com/watch?v=2WJL19wDH68&list=PLlasXeu85E9cQ32gLCvAvr9vNaUccPVNP&index=19
*/
/**
* Browser
* JavaScript Runtime Environment
* JavaScript Engine (Heart of javaScript Runtime Environment)
* Call Stack
* WEB APIS (part of RE)
* setTimeout() - timer
* DOM APIS - Dom tree
* fetch() - connect to server
* local Storage
* console
* Location
* CallBack Queue
* Event Loop
* Microtask Queue
* JS Engine uses web Apis to use these superpowers.
* We get these super powers in call stack through window (global object).
*/
// Exampe -
// It prints start
console.log("start");
// Here, setTimeout registers this call back function and starts a timer of 5 seconds
// and JavaScript continues to execute, and it prints end
// As soon as timer expires, call back function is pushed in callback queue.
// Here comes Event Loop
// Event loop keeps an eye on callback queue
// It continuously check call back queue.
// As soon as, it finds a call back function, it pushes cb function in call stack
// Then call back function is executed.
setTimeout(function cb () {
console.log("call back")
}, 5000);
console.log("end");
// Event Loop
/**
* It monitors call back queue and call stack.
* If call stack is empty, it pushes call back function in stack from microtask and callback queue.
*/
// Microtask Queue
// It has higer priority than call back queue.
// call back function of networks call(fetch) resides in microtask queue.
// All call back function comes from promises resides in microtask queue.
// All call back function comes from Mutation observer resides in microtask queue.
// First JavaScript Engine - Spider Monkey
// JavaScript Engine is a program (Application).
// JS can behave as interpreter or compiled language depends on JS Engine.
// Most modern browser uses compiler and interpreter both.
// JIT compilstion -> compiler + interpreter
// Flow of JavaScript code in JavaScript Engine
// Code -> Parsing -> Compilation -> Execution
// In parsing -
// code is broken down in tokens.
// Syntax parser generates AST (Abstract syntax tree)
// AST is output of Parsing phase.
// Now, Interprete executes code line by line and parallely comiler optimizes the code.