Event Loop and Web Performance V2 Flashcards
(148 cards)
List 3 microtasks
- Promises (resolving or rejecting)
- Mutation Observer callbacks
- process.nextTick() (Node.js)
List atleast 4 macrotasks
- setTimeout()
- setInterval()
- I/O operations (e.g. reading/writing files, making HTTP requests)
- UI rendering and user input handling
How does the event loop work in the context of this code snippet —-
console.log('Start'); setTimeout(() => console.log('Timeout'), 0); Promise.resolve().then(() => console.log('Promise')); console.log('End');
Here’s how the event loop works in this example:
- The initial synchronous task (console.log(‘Start’)) is executed and its output is printed to the console.
- The setTimeout() call is encountered and it schedules the “Timeout” callback to the macrotask queue. Since it’s a macrotask, it will be executed after all microtasks are processed.
- The Promise is encountered and it schedules its callback to the microtask queue. Since it’s a microtask, it will be executed before any macrotasks.
- The final synchronous task (console.log(‘End’)) is executed and its output is printed to the console.
- The microtask queue is checked and the Promise callback is executed, outputting “Promise”.
- The macrotask queue is checked and the “Timeout” callback is executed, outputting “Timeout”.
Explain the event loop in the context of the code snippet shown below —-
const fs = require('fs'); console.log('Start'); fs.readFile('data.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data); }); console.log('End');
In this example, we use the fs.readFile() method to read the contents of a file asynchronously. The callback function is executed when the file has been read, and it outputs the contents of the file to the console.
Here’s how the event loop works in this example:
- The initial synchronous task (console.log(‘Start’)) is executed and its output is printed to the console.
- The fs.readFile() call is encountered and it schedules an I/O operation to read the file to the OS.
- The final synchronous task (console.log(‘End’)) is executed and its output is printed to the console.
- While the OS is reading the file, the event loop is free to handle other tasks.
- When the file has been read, the OS signals the event loop to execute the callback function with the file contents.
- The callback function is executed and its output is printed to the console.
main goal of the event loop ?
the event loop allows JavaScript to execute tasks asynchronously and handle I/O operations in a non-blocking way, improving performance and responsiveness.
explain the node js event loop in here —–
~~~
console.log(‘start’);
process.nextTick(() => {
console.log(‘next tick callback’);
});
console.log(‘end’);
~~~
In this example, we use the process.nextTick() function to defer the execution of a callback until the next iteration of the event loop. We also use the console.log() function to print messages to the console.
When we run this code in Node.js, the following happens:
- The message “start” is printed to the console.
- The process.nextTick() function is called, and the callback function is added to the “next tick queue”.
- The message “end” is printed to the console.
- The current iteration of the event loop finishes, and the “next tick queue” is processed.
- The callback function is executed, and the message “next tick callback” is printed to the console.
Note that the callback function is executed after all synchronous code has finished executing, even though it was scheduled before the “end” message was printed to the console.
purpose of next tick queue
The “next tick queue” is useful for ensuring that a callback is executed after all synchronous code has finished executing, without blocking the event loop.
key difference between the event loop in Node.js and the browser
One key difference between the event loop in Node.js and the browser is the addition of the “next tick” queue. This queue allows developers to schedule a callback function to run on the next iteration of the event loop, rather than waiting for the current task to finish. This can be useful for breaking up long-running operations or for ensuring that certain code runs before other code in the event loop.
purpose of google lighthouse
audit web pages for performance, accessibility, and other best practices.
What does a google lighthouse generated report contain ?
the report contains details in regards to performance, accessibility of a web page; it also contains recommendations for improving the quality of a web page.
Why would you use a PageSpeed Insights API client library ?
Before V8 hidden classes, how were object properties stored in javascript ?
JavaScript engines used dictionaries or hash maps to store object properties
What was the turning point in the JS story that led to the birth of V8 hidden classes ?
slower property access times in regards to dictionaries / hash maps
When would V8 classes be shared amongst two or more objects ?
If multiple objects share the same structure and have the same set of properties, they can share the same hidden class, improving memory efficiency.
purpose of using V8 hidden classes
faster execution times and improved overall performance of JavaScript applications.
purpose of browser cache ?
By using the browser cache, we can improve the performance of web pages by reducing the number of requests that need to be made to the server.
Where is the browser cache located ?
on the user’s device
Which API can be used to interact with the browser cache ?
Cache API
purpose of service workers ?
Service workers are scripts that run in the background of a web page, separate from the main thread of the browser. They allow you to cache resources and handle network requests, which can improve the performance and offline functionality of web pages
purpose of local storage ?
Data storage on a browser irrespective of the fact that the browser window / browser session would stay open.
purpose of indexed DB ?
IndexedDB is a client-side database API that allows web applications to store structured data locally, with indexed access.
meaning of asynchronous API
means that all database operations are non-blocking and use callbacks or promises to handle results
Is indexed DB synchronous or asynchronous ?
async
which are the 5 caching strategies in JS?
- Indexed DB
- Local storage
- session storage
- browser cache / HTTP cache
- service workers
- Item 1
- Item 2
- Item 3
- Item 4