0
0
Node.jsframework~20 mins

Response time optimization in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response Time Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Event Loop Impact on Response Time
In Node.js, which scenario best explains why a long-running synchronous function can increase response time for incoming requests?
AThe event loop prioritizes new requests over synchronous functions, so response time is unaffected.
BThe function runs in a separate thread, so it does not affect response time.
CNode.js automatically splits synchronous functions into smaller tasks to avoid blocking.
DThe event loop is blocked, so new requests must wait until the function finishes.
Attempts:
2 left
💡 Hint
Think about how Node.js handles tasks in a single thread.
component_behavior
intermediate
2:00remaining
Effect of Async/Await on Response Time
Consider this Node.js code snippet handling HTTP requests. What is the main benefit of using async/await here for response time optimization?
Node.js
const http = require('http');

http.createServer(async (req, res) => {
  const data = await fetchDataFromDB();
  res.end(data);
}).listen(3000);
AIt allows the server to handle other requests while waiting for the database response.
BIt makes the database query run faster.
CIt blocks the event loop until the database returns data.
DIt caches the database response to reduce future response times.
Attempts:
2 left
💡 Hint
Think about what async/await does to the event loop.
🔧 Debug
advanced
2:00remaining
Identifying Cause of Slow Response in Middleware
Given this Express middleware, what is the cause of increased response time?
Node.js
app.use((req, res, next) => {
  const start = Date.now();
  while (Date.now() - start < 100) {
    // busy wait for 100ms
  }
  next();
});
AThe middleware calls next() too early, causing race conditions.
BThe middleware uses asynchronous code incorrectly, causing delays.
CThe busy wait blocks the event loop, delaying all requests.
DThe middleware does not call next(), so requests hang indefinitely.
Attempts:
2 left
💡 Hint
Consider what happens when the event loop is busy waiting.
📝 Syntax
advanced
2:00remaining
Correct Use of setImmediate for Response Time
Which option correctly uses setImmediate to defer a CPU-heavy task without blocking the response?
Node.js
app.get('/heavy', (req, res) => {
  // CPU-heavy task here
  res.end('Done');
});
A
app.get('/heavy', (req, res) =&gt; {
  heavyTask();
  setImmediate(() =&gt; res.end('Done'));
});
B
app.get('/heavy', (req, res) =&gt; {
  setImmediate(() =&gt; {
    heavyTask();
  });
  res.end('Done');
});
C
app.get('/heavy', (req, res) =&gt; {
  setTimeout(() =&gt; {
    heavyTask();
    res.end('Done');
  }, 0);
});
D
app.get('/heavy', (req, res) =&gt; {
  res.end('Done');
  heavyTask();
});
Attempts:
2 left
💡 Hint
The response should be sent immediately, and the heavy task deferred.
state_output
expert
2:00remaining
Analyzing Response Time with Caching and Async Calls
Consider this Node.js code snippet using a simple cache. What will be the output time difference between the first and second request to /data endpoint?
Node.js
const http = require('http');
let cache = null;

async function fetchData() {
  return new Promise(resolve => setTimeout(() => resolve('data'), 100));
}

http.createServer(async (req, res) => {
  if (req.url === '/data') {
    if (cache) {
      res.end(cache);
    } else {
      const data = await fetchData();
      cache = data;
      res.end(data);
    }
  } else {
    res.end('Not found');
  }
}).listen(3000);
AFirst request takes about 100ms; second request is almost instant due to cache.
BBoth requests take about 100ms because fetchData is called every time.
CFirst request is instant; second request takes 100ms because of cache delay.
DBoth requests are instant because cache is prefilled.
Attempts:
2 left
💡 Hint
Think about when fetchData is called and when cache is used.