• +91 9723535972
  • info@interviewmaterial.com

NodeJS Interview Questions and Answers

NodeJS Interview Questions and Answers

Question - 91 : - How to measure the performance of async operations?

Answer - 91 : -

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');
});

Question - 92 : - How to measure the duration of async operations?

Answer - 92 : -

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);

Question - 93 : - How are worker threads different from clusters?

Answer - 93 : -

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.

Question - 94 : - What is WASI and why is it being introduced?

Answer - 94 : -

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.

Question - 95 : - What is an Event Emitter in Node.js?

Answer - 95 : -

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');

Question - 96 : - Describe the exit codes of Node.js?

Answer - 96 : -

Exit codes give us an idea of how a process got terminated/the reason behind termination. 

A few of them are:

  • Uncaught fatal exception - (code - 1) - There has been an exception that is not handled
  • Unused - (code - 2) - This is reserved by bash
  • Fatal Error - (code - 5) - There has been an error in V8 with stderr output of the description
  • Internal Exception handler Run-time failure - (code - 7) - There has been an exception when bootstrapping function was called
  • Internal JavaScript Evaluation Failure - (code - 4) - There has been an exception when the bootstrapping process failed to return function value when evaluated.

Question - 97 : - For Node.js, why Google uses V8 engine?

Answer - 97 : -

Well, are there any other options available? Yes, of course, we have Spidermonkey from Firefox, Chakra from Edge but Google’s v8 is the most evolved(since it’s open-source so there’s a huge community helping in developing features and fixing bugs) and fastest(since it’s written in c++) we got till now as a JavaScript and WebAssembly engine. And it is portable to almost every machine known.

Question - 98 : - Why should you separate Express app and server?

Answer - 98 : -

The server is responsible for initializing the routes, middleware, and other application logic whereas the app has all the business logic which will be served by the routes initiated by the server. This ensures that the business logic is encapsulated and decoupled from the application logic which makes the project more readable and maintainable.


NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners