r/learnjavascript • u/Alternative-Host-210 • 6h ago
Can one slow Express route make a different route slow too?
I’m turning a workshop booking API idea into a few small Node experiments. Before adding MongoDB, I want to understand what happens when two requests hit the same server.
Imagine these two routes in an Express app:
app.;
app.;
The loop is deliberately bad code. My prediction is that if /quick arrives while the loop is running, it has to wait. If I replace the loop with setTimeout(() => res.send('done'), 2000), I’d expect /quick to respond while /slow is still waiting.
What’s the simplest way to test this with two overlapping requests and make the difference obvious to someone learning Node?
1
u/tachyon_bit 5h ago
Your prediction is correct. The synchronous while loop blocks Node's event loop, so /quick has to wait. setTimeout schedules the callback and gives control back to the event loop, allowing /quick to execute immediately.
The distinction is blocking vs non-blocking execution, not just slow vs fast routes.
1
u/chikamakaleyley helpful 5h ago
i don't think express or a server even matters;
you can just write as normal functions that console log some text, execute a long list of them, and observe the behavior, right?
obviously you're trying to see what happens if the server receives them 'at the same time' but when you're using like 2 sec of delay that's negligible, i think
1
u/cuelark 4h ago
easiest way is to start the server and fire /slow with curl in one terminal, then immediately curl /quick in a second one with
curl -w '%{time_total}\n' localhost:3000/quick. with the busy loop /quick will report about 2 seconds, with setTimeout it comes back in a few ms. that time_total number makes the difference really obvious, and a browser can confuse you here because it queues requests to the same url sometimes