Identify the primary performance issue in the following code snippet.

Node.js interview question for Advanced practice.

Answer

The heavyComputation function is synchronous and will block the event loop, causing the application to become unresponsive.

Explanation

The main problem is that heavyComputation is a long-running, synchronous (blocking) operation. Because setInterval executes on the main thread, each call to heavyComputation will block the entire Node.js event loop. If the computation takes longer than the interval (1000ms), the callbacks will stack up, and the application will be unable to handle any other requests or events.

Related Questions