How Js Works: Memory Management Concurrency in JS?
How Js Works: Memory Management Concurrency in JS?
Memory management
Concurrency in JS?
Memory management
Memory life cycle
Gargabe collection
Memory leaks
Memory life cycle
Allocation and use
• JS Allocates memory alongside declaring values.
• That memory belongs to its scope (Memory heap).
// filters.module.js
var a = 2; // access by global export function filterByText(ownerId, ownerName) {
function f() { return (text) => {
var a = 3; const nameFilter = {$regex : `.*${text}.*`};
console.log(a); // prints 3 const query = { owner: ownerId, name : filter};
} return Folders.find(query).map(
f(); // Using ({ name }) => ({ name, owner: ownerName })
);
}
}
f();
GC demo with closure
Memory leaks
• Global variables const interval = setInterval(() => {
// any code
• Timers or intervals that are forgotten }, 5000);
// clearInterval(interval); Forget remove
• Closures (meteor forum)
• Out of DOM references var elements = {
button: document.getElementById('button'),
image: document.getElementById('image'),
cell: document.getElementById('someCell'),
• RxJS Live subscriptions };
function removeTable() {
const table = document.getElementById('aTable');
document.body.removeChild(table);
}
RxJS subscriptions
• ReactiveX is a library for composing asynchronous and event-based
programs by using observable sequences.
• Demo
• Solution
• ngUnsubscribe // OnDestroy hook
Concurrency in JS?
Concurrency Model and Event Loop
JS Runtime
Demo
JS is Concurrent, but isn't Parallel
• Concurrency is about composing independent processes to work
together
• Parallelism is about actually executing multiple processes
simultaneously
Concurrency model and Event Loop
• JavaScript is a single-threaded programming language. Therefore it
can do one thing at a time
• Use one Call Stack to trace the execution of a script and Memory
Heap for memory allocation.
function multiply(x, y) {
return x * y;
}
function printSquare(x) {
var s = multiply(x, x);
console.log(s);
}
printSquare(5);
Runtime
angular
JS Engine
RxJS
e.g. V8
Chrome
NodeJS
Demo
http://latentflip
.com/loupe
Search demo example debug it
Threat Detail of MSP example
404
Sequence diagram not found
Go DEMO
Preguntas?
Resources
• Memory management
• https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop
• https://blog.sessionstack.com/how-javascript-works-memory-
management-how-to-handle-4-common-memory-leaks-3f28b94cfbec
• https://msdn.microsoft.com/en-us/library/ms976398.aspx
• http://arindam89.github.io/JSTalk_ScopeChains/index.html#
• Concurrency
• https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop
• https://blog.sessionstack.com/how-javascript-works-event-loop-and-the-
rise-of-async-programming-5-ways-to-better-coding-with-2f077c4438b5
• https://danmartensen.svbtle.com/events-concurrency-and-javascript
• http://latentflip.com/loupe
• http://nikgrozev.com/2017/10/01/async-await/