This session was organized today at Zebpay office in Mumbai along with team joining from Ahmedabad.
I discussed the basic core principals of js and slowly moved towards the advance concepts, code examples of closures and NodeJS.
We also looked at JS runtime visualization using Loupe.
The team was very excited and interactive. They loved it and supported for future such discussions.
Enjoyed the evening today with the team!
8. How JS Works
● Almost everyone has already heard of the V8 Engine as a concept, and most people
know that JavaScript is single-threaded or that it is using a callback queue.
● A popular example of a JavaScript Engine is Google’s V8 engine. The V8 engine is used
inside Chrome and Node.js for example. Here is a very simplified view of what it looks
like:
9. Memory Heap — this is where the memory allocation happens
Call Stack — is a data structure which records basically where in the program we are
10. There are APIs in the browser that have been used by almost any JavaScript developer out
there (e.g. “setTimeout”). Those APIs, however, are not provided by the Engine.
So, where are they coming from?
It turns out that the reality is a bit more complicated.
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. CLOSURES
What happens when a function is executed outside
of it's original scope chains?
Moreover, what happens when a variable holds on
to a function reference which get's defined in such
deep function execution chain?
48. So What is Closures?
It is an implicit, permanent link between a function
and it's scope chain.. A function definition's (lambda)
hidden [[scope]] reference.
● Holds the scope chain (preventing garbage
collection.)
● It is used and copied as the "outer environment
reference" anytime the function is run.
49. Implicit Closure
var data = "My Data!";
setTimeout(function() {
console.log(data); // prints "My Data!"
}, 3000);
50.
51. Event Loop And Async Programming
Enough slides, let’s go directly to the demo and see
it in action-
http://localhost:3051
52.
53.
54.
55.
56. NodeJS in Brief
● Server-side Javascript
● Built on Google’s V8
● Evented, non-blocking I/O. Similar to
● EventMachine or Twisted.
● CommonJS module system.
58. Many web applications have code
like this:
var result = db.query("select * from Zebay_COunterOrders");
// use result here
59. Many web applications have code
like this:
var result = db.query("select * from Zebay_COunterOrders");
// use result here
What is the software doing while it
queries the database?
60. Many web applications have code
like this:
var result = db.query("select * from Zebay_COunterOrders");
// use result here
What is the software doing while it
queries the database?
In many cases, just waiting for the
response.
66. Apache vs NGINX
The difference?
Apache uses one thread per Connection.
NGINX doesn’t use threads. It uses an event loop.
67. Context switching is not free
Execution stacks take up memory
For massive concurrency, cannot use an OS thread
for each connection.
68. Code like this
var result = db.query("select * from Zebay_COunterOrders");
either blocks the entire process or implies multiple
execution stacks.
69. But a line of code like this
var result = db.query("select * from Zebay_COunterOrders", functon(error, data){
// use data
});
allows the program to return to the event loop
immediately.