Explore topic-wise InterviewSolutions in .

This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.

1.

How to measure the performance of async operations?

Answer»

Performance API provides us with tools to FIGURE out the necessary performance metrics. 

A simple EXAMPLE would be:

CONST { PerformanceObserver, performance } = require('perf_hooks');const obs = new PerformanceObserver((items) => { console.log(items.getEntries()[0].DURATION); performance.clearMarks();});obs.observe({ entryTypes: ['measure'] });performance.measure('Start to Now');performance.mark('A');doSomeLongRunningProcess(() => { performance.measure('A to Now', 'A'); performance.mark('B'); performance.measure('A to B', 'A', 'B');});
2.

How to measure the duration of async operations?

Answer»

Performance API PROVIDES us with tools to figure out the necessary performance metrics. A simple example would be using async_hooks and perf_hooks

'use STRICT';const async_hooks = REQUIRE('async_hooks');const { performance, PerformanceObserver} = require('perf_hooks');const set = new Set();const hook = async_hooks.createHook({ init(id, type) {if (type === 'Timeout') { performance.mark(`Timeout-${id}-Init`); set.add(id);} }, destroy(id) {if (set.has(id)) { set.delete(id); performance.mark(`Timeout-${id}-Destroy`); performance.measure(`Timeout-${id}`, `Timeout-${id}-Init`, `Timeout-${id}-Destroy`);} }});hook.enable();const obs = new PerformanceObserver((list, observer) => { console.log(list.getEntries()[0]); performance.clearMarks(); observer.disconnect();});obs.observe({ entryTypes: ['measure'], buffered: true });SETTIMEOUT(() => {}, 1000);

This would give us the EXACT time it took to execute the callback.

3.

How are worker threads different from clusters?

Answer»

Cluster:

  • There is one process on each CPU with an IPC to communicate.
  • In case we WANT to have multiple servers accepting HTTP requests via a single PORT, clusters can be helpful.
  • The processes are spawned in each CPU THUS will have separate memory and node instance which further will lead to memory issues.

Worker threads:

  • There is only one process in total with multiple threads.
  • Each thread has one Node instance (one EVENT loop, one JS engine) with most of the APIs accessible.
  • Shares memory with other threads (e.g. SharedArrayBuffer)
  • This can be used for CPU-intensive tasks like processing data or ACCESSING the file system since NodeJS is single-threaded, synchronous tasks can be made more efficient leveraging the worker's threads.
4.

What is WASI and why is it being introduced?

Answer»

Web assembly provides an implementation of WebAssembly System Interface specification through WASI API in node.js implemented using WASI CLASS. The introduction of WASI was done by keeping in mind its possible to USE the underlying OPERATING system VIA a collection of POSIX-like FUNCTIONS thus further enabling the application to use resources more efficiently and features that require system-level access.

5.

What is a thread pool and which library handles it in Node.js

Answer»

The Thread pool is handled by the libuv library. libuv is a multi-platform C library that provides SUPPORT for asynchronous I/O-based OPERATIONS such as FILE SYSTEMS, networking, and concurrency. 

Thread Pool
6.

Enhancing Node.js performance through clustering.

Answer»

Node.js APPLICATIONS run on a single processor, which means that by default they don’t take advantage of a multiple-core system. Cluster mode is used to start up multiple node.js processes thereby having multiple instances of the EVENT LOOP. When we start USING cluster in a nodejs app behind the scene multiple node.js processes are created but there is also a parent process called the cluster manager which is responsible for monitoring the HEALTH of the individual instances of our application.

Clustering in Node.js
7.

What is an Event Emitter in Node.js?

Answer»

EventEmitter is a Node.js class that INCLUDES all the objects that are basically capable of emitting events. This can be done by attaching named events that are emitted by the object using an eventEmitter.on() function. Thus whenever this object throws an even the attached FUNCTIONS are INVOKED synchronously.

const EventEmitter = require('events');class MyEmitter extends EventEmitter {}const myEmitter = NEW MyEmitter();myEmitter.on('event', () => { console.log('an event OCCURRED!');});myEmitter.emit('event');