Node.js Fundamentals Flashcards

Node Architecture & Event Loop Modules (CommonJS, ES Modules) Process & Buffer File System & Streams Error Handling (68 cards)

1
Q

What is the Node.js architecture?

A

Node.js uses a single-threaded event loop architecture with non-blocking I/O operations to handle concurrent client requests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the event loop in Node.js?

A

The event loop in Node.js is the mechanism that handles asynchronous operations by executing non-blocking I/O operations on a single thread.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Advantages of Node.js event loop architecture?

A

Improved performance for I/O-heavy tasks, high concurrency, low memory consumption, fast handling of numerous simultaneous requests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Disadvantages of Node.js event loop architecture?

A

Can be inefficient for CPU-heavy tasks, may lead to blocking of the event loop if not properly managed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Best practices for Node.js event loop?

A

Offload CPU-intensive tasks to worker threads, use asynchronous I/O, avoid blocking the event loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Use cases for Node.js event loop?

A

Real-time applications (chat, games), microservices, API backends, streaming services.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Impact of Node.js event loop on system design?

A

It enables high throughput and scalability, but requires careful management of blocking operations to avoid bottlenecks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Architectural implications of Node.js event loop?

A

Allows building highly scalable systems, but requires balancing CPU-heavy tasks and offloading them to avoid event loop blockages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Performance implications of Node.js event loop?

A

Good for handling high volumes of I/O-bound requests, but less suited for CPU-heavy tasks which might slow down other requests.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Fault tolerance in Node.js event loop?

A

Single-threaded architecture makes it susceptible to errors affecting the whole application. Use clustering and worker threads for fault tolerance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Monitoring and debugging Node.js event loop?

A

Use tools like Node.js built-in profiler, clinic.js, or node-inspect to monitor event loop performance and detect potential bottlenecks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Common interview question about the event loop?

A

Explain the event loop in Node.js and how it handles concurrency.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Potential gotchas with Node.js event loop?

A

Blocking the event loop with synchronous code or unoptimized CPU-heavy tasks can degrade performance and cause unresponsiveness.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is CommonJS in Node.js?

A

CommonJS is the module system used in Node.js, allowing you to export and import modules using module.exports and require().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is ES Modules in Node.js?

A

ES Modules is the standard JavaScript module system that uses import and export syntax, introduced for modern JavaScript.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Advantages of CommonJS?

A

Works synchronously and is widely supported in Node.js, simple to use with require() and module.exports.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Advantages of ES Modules?

A

Supports import/export syntax, allows static analysis and tree shaking, and is the official module system for JavaScript.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Disadvantages of CommonJS?

A

Synchronous loading, can lead to issues when working with asynchronous code or in browser environments.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Disadvantages of ES Modules?

A

Not supported natively in all Node.js versions until recently, has potential compatibility issues with CommonJS modules.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Best practices for using modules in Node.js?

A

Use ES Modules for new projects where possible, use CommonJS for compatibility, prefer named exports over default exports.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Use cases for CommonJS?

A

Widely used in Node.js for server-side applications, packages, and libraries.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Use cases for ES Modules?

A

Client-side JavaScript, modern Node.js apps, projects using tree shaking and bundling tools.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Architectural implications of using CommonJS?

A

CommonJS allows dynamic module loading, but can cause issues with synchronous imports in larger applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Performance implications of using ES Modules?

A

ES Modules enable better optimization, like tree shaking, for bundling and reduce the overall app size.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Fault tolerance with modules?
In Node.js, fault tolerance can be enhanced by structuring code into well-defined modules, catching errors in module boundaries.
26
Monitoring and debugging modules?
Use `console.log()` to check module imports and exports, tools like `node --inspect` help debug the module system.
27
Common interview question about modules?
What is the difference between CommonJS and ES Modules in Node.js?
28
Potential gotchas with modules?
Mixing CommonJS and ES Modules can lead to compatibility issues, be mindful of async vs sync loading behaviors.
29
What is a Node.js process?
A Node.js process is an instance of the Node runtime, it is the environment where JavaScript code is executed.
30
What is the Buffer class in Node.js?
Buffer is a built-in object in Node.js used to handle raw binary data directly in memory, without needing to deal with string encoding.
31
Advantages of using Buffer?
Efficient for working with binary data, avoids encoding overhead when reading or writing files or data streams.
32
Disadvantages of using Buffer?
Can lead to memory issues if large buffers are not managed correctly, difficult to handle for large data processing.
33
Best practices for using Buffer?
Always allocate the right size for buffers, release memory when no longer needed, use `Buffer.alloc()` instead of `new Buffer()` to avoid security risks.
34
Use cases for Buffer?
Handling binary data, working with streams, interacting with binary file formats, communication with hardware.
35
Architectural implications of Process and Buffer?
Proper memory management is crucial, improper handling of buffers can lead to memory leaks or crashes in long-running processes.
36
Performance implications of Buffer?
Buffers are efficient for dealing with raw data, but allocating large buffers without managing memory can lead to performance degradation.
37
Fault tolerance in Buffer?
Handle buffer allocation errors gracefully, ensure that buffer sizes do not exceed memory limits, avoid over-allocating memory.
38
Monitoring and debugging Process & Buffer?
Use Node.js built-in process monitoring tools like `process.memoryUsage()`, check buffer sizes and allocations during debugging.
39
Common interview question about Process and Buffer?
How does Node.js handle memory management and what are the differences between Buffers and strings?
40
Potential gotchas with Process and Buffer?
Not freeing up buffers or managing large memory allocations can cause memory leaks or crashes.
41
What is the File System module in Node.js?
The File System (fs) module allows interaction with the file system, enabling reading, writing, and managing files.
42
What are Streams in Node.js?
Streams in Node.js are abstract interfaces for handling streaming data, which can be read or written in chunks.
43
Advantages of using File System module?
Enables efficient file handling, supports both synchronous and asynchronous operations, and works with large files.
44
Disadvantages of using File System module?
Synchronous file operations can block the event loop, leading to performance degradation in high-concurrency applications.
45
Advantages of using Streams?
Efficient memory usage, allows for processing of large files or data in chunks without loading everything into memory.
46
Disadvantages of using Streams?
Complexity in managing stream flows, may require handling backpressure when writing large volumes of data.
47
Best practices for using Streams?
Use streams for large data handling, implement proper error handling, manage backpressure to avoid memory overflow.
48
Use cases for File System?
Reading and writing large files, managing configurations, logging, and database storage.
49
Use cases for Streams?
Video/audio streaming, reading large files, piping data between processes or services.
50
Architectural implications of File System and Streams?
Streams help reduce memory usage, but they require careful error and backpressure management.
51
Performance implications of Streams?
Streams improve performance when dealing with large datasets, as they avoid loading everything into memory.
52
Fault tolerance with Streams?
Handle errors in streams (e.g., file not found) and backpressure to prevent crashes or memory issues.
53
Monitoring and debugging File System and Streams?
Monitor the status of streams with events like `data`, `end`, and `error`, use logging to monitor file read/write operations.
54
Common interview question about Streams?
What is the difference between streams and buffers in Node.js?
55
Potential gotchas with File System and Streams?
Improper error handling or not managing backpressure properly can result in data loss or crashes.
56
What is error handling in Node.js?
Error handling in Node.js involves using try/catch blocks for synchronous code and handling asynchronous errors with callbacks or promises.
57
What are the common strategies for error handling in Node.js?
Use `try/catch` for synchronous errors, `callback` with error-first argument for asynchronous errors, and `Promise.catch()` for promise rejections.
58
Advantages of proper error handling?
Improved reliability, prevents unhandled errors from crashing the application, better debugging and logging.
59
Disadvantages of improper error handling?
Can lead to unhandled exceptions, crashes, security vulnerabilities, or data loss.
60
Best practices for error handling in Node.js?
Always handle asynchronous errors, log meaningful error messages, provide custom error classes for better debugging.
61
Use cases for error handling?
Handling failed API requests, file reading errors, and database connection failures.
62
Impact of error handling on system design?
Error handling ensures graceful degradation and robustness, leading to fault-tolerant systems.
63
Architectural implications of error handling?
Centralized error handling, use of logging systems for tracking errors, defining custom error types.
64
Performance implications of error handling?
Improper handling can lead to unnecessary retries or performance bottlenecks, excessive logging might degrade performance.
65
Fault tolerance in error handling?
Gracefully handle errors by retrying or falling back to alternate systems, provide fallbacks to maintain service.
66
Monitoring and debugging error handling?
Use logging frameworks like `winston` or `bunyan` to capture and monitor errors, integrate with monitoring systems.
67
Common interview question about error handling?
How would you handle errors in a Node.js application?
68
Potential gotchas with error handling?
Not handling asynchronous errors properly can lead to uncaught exceptions, causing application crashes.