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
@@ -45,6 +43,8 @@ JavaScript was originally designed for the browser, so interpreters were ideal f
45
43
<b>Pros:</b> Quick to get up and running </br>
46
44
<b>Cons:</b> Slow at executing loops and repetitive code
47
45
46
+
---
47
+
48
48
## Compiler
49
49
50
50
Compilers essentially translate our code and compile it down into machine language.
@@ -64,19 +64,19 @@ Both of these do exactly what compilers do: Take one language and convert into a
64
64
65
65
</i>
66
66
67
-
##JIT Compiler
67
+
### <b>JIT Compiler</b>
68
68
69
69
JIT (Just in time) compilers are essentially the combination of an interpreter and a compiler. They were designed to make JS Engines run faster.
70
70
71
-
<br>
71
+
---
72
72
73
73
## ECMAScript
74
74
75
75
ECMAScript is the governing body which sets the standardised rules for JS engines
76
76
77
77
[List of ECMAScript engines](https://en.wikipedia.org/wiki/List_of_ECMAScript_engines)
78
78
79
-
<br>
79
+
---
80
80
81
81
## <i>"JS is an interpreted language"</i>
82
82
@@ -90,8 +90,7 @@ Initially when JavaScript first came out, engines such as Spider Monkey only use
90
90
91
91
Today, modern JS Engines have evolved to include compilers for optimisation.
92
92
93
-
<br>
94
-
<hr>
93
+
---
95
94
96
95
## Writing optimised code
97
96
@@ -105,11 +104,15 @@ Be careful when using the following functions as they can affect the JS engine's
105
104
- with
106
105
- delete
107
106
108
-
### Inline Caching
107
+
---
108
+
109
+
## Inline Caching
109
110
110
111
Inline caching is an optimization technique employed by V8. The goal is to speed up runtime method binding by remembering the results of a previous method lookup directly at the call site. JavaScript engines use IC to memorize information on where to find properties on objects, to reduce the number of timely lookups.
111
112
112
-
### Hidden Classes
113
+
---
114
+
115
+
## Hidden Classes
113
116
114
117
JavaScript is a dynamic language. Data type is determined at the time of execution, making accessing object properties fairly slow, in comparison to statically typed languages. It’s rather hard to optimize a dynamically typed language. Objects can change their type during runtime and it happens implicitly. To track types of JavaScript object and variables, V8 introduced the concept of hidden classes. During runtime V8 creates hidden classes that get attached to each and every object containing memory for each property. This allows for V8 to optimize the property access time.
115
118
@@ -120,8 +123,7 @@ It's important to write performant, predictable code which doesn't impede the en
120
123
- Assign all properties of an object within it's constructor
121
124
- Instantiate object properties in the same order
122
125
123
-
<br>
124
-
<hr>
126
+
---
125
127
126
128
## Call Stack
127
129
@@ -134,31 +136,33 @@ You can think of the call stack as a region in memory which operates in 'first i
134
136
- When the current function is finished, the interpreter takes it off the stack and resumes execution where it left off in the last code listing.
135
137
- If the stack takes up more space than it had assigned to it, it results in a "stack overflow" error.
136
138
137
-
## Memory Heap
139
+
---
140
+
141
+
## <b>Memory Heap</b>
138
142
139
143
In most native executable programs, there are two types of memory available: stack-based and heap-based memory.
140
144
141
145
The heap is reserved for the memory allocation needs of the program. It is an area apart from the program code and the stack which stores program data in an unordered fashion.
142
146
143
-
### Garbage Collection
147
+
### <b>Garbage Collection</b>
144
148
145
149
JavaScript is a garbage collected language. Data stored in the memory heap that isn't needed anymore gets cleared away to preserve memory. This helps prevent memory leaks.
146
150
147
151
Garbage collection in JS uses the 'mark and sweep' algorithm. Essentially it marks data which is being used and needs to be kept, and sweeps away data which is no longer being used or referenced.
148
152
149
-
##Memory Leaks
153
+
### <b>Memory Leaks</b>
150
154
151
155
A memory leak occurs when a computer program incorrectly manages memory allocations in such a way that memory which is no longer needed is not released. This is often due to unintentional references from other objects. Memory leaks in web pages often involve interaction between JavaScript objects and DOM elements.
152
156
153
157
A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.
154
158
155
-
Common causes of memory leaks to avoid:
159
+
<b>Common causes of memory leaks to avoid:</b>
156
160
157
161
- <b>Global variables:</b> Using too many global variables can add excessive amounts of data to memory which doesn't get cleared away.
158
162
- <b>Event Listeners:</b> Unregistered event listeners can cause memory leaks because any objects referenced by them can't be garbage collected. Memory leaks often occur after a lot of user interaction.
159
163
- <b>Set Interval:</b> Unless stopped, the objects within setInterval() are never garbage collected becuase the function keeps running.
160
164
161
-
<br>
165
+
---
162
166
163
167
## JavaScript Runtime
164
168
@@ -172,15 +176,15 @@ The JS Runtime Environment executes asynchronous code in the background and uses
172
176
173
177
All modern browsers have a JS engine as well as a JS runtime which provides a web API.
174
178
175
-
### Web Browser API:
179
+
### <b>Web Browser API</b>
176
180
177
181
Web API's are applications provided by the browser which can do a variety of tasks in the background such as sending HTTP requests, listening to DOM events, caching and database storage on the browser.
178
182
179
-
### The Heap
183
+
### <b>The Heap</b>
180
184
181
185
The first container in the environment, which is also part of the V8 JS Engine, is called the ‘memory heap.’ As the V8 JS Engine comes across variables and function declarations in the code it stores them in the Heap.
182
186
183
-
### The Stack
187
+
### <b>The Stack</b>
184
188
185
189
The second container in the environment is called the ‘call stack.’ It is also part of the V8 JS Engine. As the JS Engine comes across an actionable item, like a function call, it adds it to the Stack.
186
190
@@ -190,24 +194,30 @@ When a function returns a value, or is sent to the Web API container, it is popp
190
194
191
195
<i>Note - the Stack is a data structure that runs LIFO — last in first out. No function other than the one at the top of the stack will ever be in focus, and the engine will not move to the next function unless the one above it is popped off.</i>
192
196
193
-
### The Web API Container
197
+
### <b>The Web API Container</b>
194
198
195
199
The Web API calls that were sent to the Web API container from the Stack, like event listeners, HTTP/AJAX requests, or timing functions, sit there until an action is triggered. Either a ‘click’ happens, or the HTTP request finishes getting its data from its source, or a timer reaches its set time. In each instance, once an action is triggered, a ‘callback function’ is sent to the fourth and final container, the ‘callback queue.’
196
200
197
-
### The Callback Queue
201
+
### <b>The Callback Queue</b>
198
202
199
203
The Callback Queue will store all the callback functions in the order in which they were added. It will ‘wait’ until the Stack is completely empty. When the Stack is empty it will send the callback function at the beginning of the queue to the Stack. When the Stack is clear again, it will send over its next callback function.
200
204
201
205
<i>Note - the Queue is a data structure that runs FIFO — first in first out. Whereas the Stack uses a push and pop (add to end take from end), the Queue uses push and shift (add to end take from beginning).</i>
202
206
203
-
### The Event Loop
204
-
205
-
The Event Loop can be thought of as a ‘thing’ inside the javascript runtime environment. Its job is to constantly look at the Stack and the Queue. If it sees the Stack is empty, it will notify the Queue to send over its next callback function. The Queue and the Stack might be empty for a period of time, but the event loop never stops checking both. At any time a callback function can be added to the Queue after an action is triggered from the Web API container.
207
+
### <b>The Event Loop</b>
206
208
207
-
<b>This is what they mean when they say Javascript can run asynchronously. It isn’t actually true, it just seems true. Javascript can only ever execute one function at a time, whatever is at top of the stack, it is a synchronous language. But because the Web API container can forever add callbacks to the queue, and the queue can forever add those callbacks to the stack, we think of javascript as being asynchronous. This is really the great power of the language. Its ability to be synchronous, yet run in an asynchronous manner, like magic!</b>
209
+
The Event Loop can be thought of as a ‘thing’ inside the javascript runtime environment. It's job is to constantly look at the Stack and the Queue. If it sees the Stack is empty, it will notify the Queue to send over its next callback function. The Queue and the Stack might be empty for a period of time, but the event loop never stops checking both. At any time a callback function can be added to the Queue after an action is triggered from the Web API container.
208
210
209
211
<br>
210
212
213
+
<b>This is what they mean when they say Javascript can run asynchronously. It isn’t actually true, it just seems true.
214
+
215
+
Javascript can only ever execute one function at a time, whatever is at top of the stack, it is a synchronous language. But because the Web API container can forever add callbacks to the queue, and the queue can forever add those callbacks to the stack, we think of javascript as being asynchronous.
216
+
217
+
This is really the great power of the language. Its ability to be synchronous, yet run in an asynchronous manner, like magic!</b>
218
+
219
+
---
220
+
211
221
## Node.js
212
222
213
223

@@ -236,8 +246,7 @@ The main difference between the Node.js approach and a Python threaded-server is
236
246
237
247
In contrast, the Node.js approach is to schedule callbacks that are to be invoked once the slow thing is done. Node.js' single thread is never sleeping except if there are literally no requests to process. The code runs in a persistent context that exists as long as your Node.js server is running.
238
248
239
-
<br>
240
-
<hr>
249
+
---
241
250
242
251
## Lexical Environment
243
252
@@ -247,13 +256,17 @@ In JavaScript our lexical scope (available data & variables where the function w
Scope of a variable/function is basically the locations from where a variable is visible/accessible. In JavaScript there are two types of scope:
253
264
254
265
- <b>Local scope:</b> Variables defined inside a function are not accessible (visible) from outside the function. They become LOCAL to the function. JavaScript has function scope: Each function creates a new scope.
255
266
- <b>Global scope:</b> A variable declared outside a function, becomes GLOBAL. A global variable has global scope: All scripts and functions on a web page can access it.
256
267
268
+
---
269
+
257
270
## Execution Context
258
271
259
272
Execution context is defined as the environment in which JavaScript code is executed. In JavaScript environment there are 2 main types of Execution Context:
@@ -272,7 +285,9 @@ The execution context/environment of a function roughly equates to:
272
285
- The scope chain of that function i.e. variables and objects available for the function to use
273
286
- Value of `this`
274
287
275
-
### Hoisting
288
+
---
289
+
290
+
## Hoisting
276
291
277
292
Hoisting is the behavior of moving variables or function declarations to the top of their respective environments during the compilation phase. The JavaScript engine allocates memory for the variables and functions that it sees in your code during the creation phase, even before the code is run.
278
293
@@ -311,6 +326,8 @@ But it's important to remember that during this creation phase we also have this
311
326
312
327
Execution context works as a unit in the overall flow of execution. Since every function has its own execution context, the JS compiler maintains a stack of execution contexts. It tracks whether the execution contexts are in the correct order. The top of the stack contains the execution context of the function that is currently being executed.
313
328
329
+
---
330
+
314
331
## Scope Chain
315
332
316
333
Every scope is always connected to one or more scopes in their back forming a chain or a hierarchy, except the global scope. The global scope does not have any parent, and that makes sense too since it is sitting at the top of the hierarchy.
@@ -470,3 +487,29 @@ truck.fuel; // => 160
470
487
<i>Summary:</i>
471
488
472
489
`.call()` and `.apply()` are useful for borrowing methods from an object, while `.bind()` is useful to call functions later on with a certain context or a certain `this` keyword.
490
+
491
+
---
492
+
493
+
## Function Currying
494
+
495
+
Currying is the process of taking a function with multiple arguments and returning a series of functions that take one argument and eventually resolve to a value.
496
+
497
+
```javascript
498
+
functionmultiply(a, b) {
499
+
return a * b;
500
+
}
501
+
502
+
let multiplyByTwo =multiply.bind(this, 2);
503
+
504
+
multiplyByTwo(4); // => 8
505
+
```
506
+
507
+
---
508
+
509
+
## Context vs Scope
510
+
511
+
Scope pertains to the visibility of variables, and context refers to the object to which a function belongs.
512
+
513
+
<b>Scope</b> has to do with the the visibility of variables. In JavaScript, scope is achieved through the use of functions. When you use the keyword `var` inside of a function, the variable that you are initializing is private to the function, and cannot be seen outside of that function.
514
+
515
+
<b>Context</b> is related to objects. It refers to the object to which a function belongs. When you use the JavaScript `this` keyword, it refers to the object to which the function belongs.
0 commit comments